1. 编写2个APP应用

在 Streamlit 中一个 .py 文件就是一个 APP 应用,下面我们在 Nginx 上部署 2 个 Streamlit APP:

:::tips 📑 hello.py :::

  1. import streamlit as st
  2. import numpy as np
  3. import time
  4. progress_bar = st.progress(0)
  5. status_text = st.empty()
  6. chart = st.line_chart(np.random.randn(10, 2))
  7. for i in range(100):
  8. # Update progress bar.
  9. progress_bar.progress(i + 1)
  10. new_rows = np.random.randn(10, 2)
  11. # Update status text.
  12. status_text.text(
  13. 'The latest random number is: %s' % new_rows[-1, 1])
  14. # Append data to the chart.
  15. chart.add_rows(new_rows)
  16. # Pretend we're doing some computation that takes time.
  17. time.sleep(0.1)
  18. status_text.text('Done!')
  19. st.balloons()

:::tips 📑 hello2.py :::

  1. import streamlit as st
  2. import numpy as np
  3. import pandas as pd
  4. if st.checkbox('Show dataframe'):
  5. chart_data = pd.DataFrame(
  6. np.random.randn(20, 3),
  7. columns=['a', 'b', 'c'])
  8. chart_data

2. nohup启动APPs

Streamlit 需要在单独启动每一个 APP 应用:

  1. $ nohup streamlit run --server.port 8051 hello.py > /dev/null 2>&1 &
  2. $ nohup streamlit run --server.port 8052 hello2.py > /dev/null 2>&1 &

3. Nginx设置反向代码

  1. server {
  2. listen 8081;
  3. server_name localhost;
  4. location /hello {
  5. rewrite /hello/(.*) /$1 break;
  6. proxy_pass http://localhost:8501;
  7. proxy_http_version 1.1;
  8. proxy_buffering off;
  9. proxy_set_header Upgrade $http_upgrade;
  10. proxy_set_header Connection "upgrade";
  11. proxy_read_timeout 86400;
  12. }
  13. location /hello2 {
  14. rewrite /hello2/(.*) /$1 break;
  15. proxy_pass http://localhost:8502;
  16. proxy_http_version 1.1;
  17. proxy_buffering off;
  18. proxy_set_header Upgrade $http_upgrade;
  19. proxy_set_header Connection "upgrade";
  20. proxy_read_timeout 86400;
  21. }

注意:由于 Streamlit 采用的是 Tornado 框架,所以无法像 Django、Flask 一样结合 uWSGI 进行部署。

4. 访问

这样你就可以访问 http://localhost:8081/hello/ http://localhost:8081/hello2/ 来访问 2 个应用了,当来你使用 http://localhost:8501http://localhost:8502 也是可以访问得到应用的。

5. 参考文档