1. 编写2个APP应用
在 Streamlit 中一个 .py 文件就是一个 APP 应用,下面我们在 Nginx 上部署 2 个 Streamlit APP:
:::tips 📑 hello.py :::
import streamlit as stimport numpy as npimport timeprogress_bar = st.progress(0)status_text = st.empty()chart = st.line_chart(np.random.randn(10, 2))for i in range(100):# Update progress bar.progress_bar.progress(i + 1)new_rows = np.random.randn(10, 2)# Update status text.status_text.text('The latest random number is: %s' % new_rows[-1, 1])# Append data to the chart.chart.add_rows(new_rows)# Pretend we're doing some computation that takes time.time.sleep(0.1)status_text.text('Done!')st.balloons()
:::tips 📑 hello2.py :::
import streamlit as stimport numpy as npimport pandas as pdif st.checkbox('Show dataframe'):chart_data = pd.DataFrame(np.random.randn(20, 3),columns=['a', 'b', 'c'])chart_data
2. nohup启动APPs
Streamlit 需要在单独启动每一个 APP 应用:
$ nohup streamlit run --server.port 8051 hello.py > /dev/null 2>&1 &$ nohup streamlit run --server.port 8052 hello2.py > /dev/null 2>&1 &
3. Nginx设置反向代码
server {listen 8081;server_name localhost;location /hello {rewrite /hello/(.*) /$1 break;proxy_pass http://localhost:8501;proxy_http_version 1.1;proxy_buffering off;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";proxy_read_timeout 86400;}location /hello2 {rewrite /hello2/(.*) /$1 break;proxy_pass http://localhost:8502;proxy_http_version 1.1;proxy_buffering off;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";proxy_read_timeout 86400;}
注意:由于 Streamlit 采用的是 Tornado 框架,所以无法像 Django、Flask 一样结合 uWSGI 进行部署。
4. 访问
这样你就可以访问 http://localhost:8081/hello/ 和 http://localhost:8081/hello2/ 来访问 2 个应用了,当来你使用 http://localhost:8501 和 http://localhost:8502 也是可以访问得到应用的。
