部署-Nginx部署多个StreamlitAPP-《Python教程》

admin 2025-10-20 00:16:29 编程 来源:ZONE.CI 全球网 0 阅读模式

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. 参考文档

以太坊cppgolang区别 编程

以太坊cppgolang区别

以太坊是一种去中心化的开源平台,它采用智能合约技术,旨在构建和运行不受干扰的分布式应用程序。作为目前最受欢迎的区块链平台之一,以太坊提供了多种编程语言的支持,其
progolang 编程

progolang

Go语言(Golang)是由Google开发的一门静态类型编程语言。作为一名专业的Golang开发者,我深知这门语言的优势和特点。在本文中,我将介绍Golang
golangn个发送者 编程

golangn个发送者

Golang是一种开源的编程语言,由Google团队开发,旨在提高程序的并发性和简化软件开发过程。在Go语言中,有时需要向多个接收者发送信息。本文将介绍如何在G
golang技能图谱 编程

golang技能图谱

从互联网行业的快速发展到人工智能技术的日益成熟,各种编程语言也应运而生。而在这众多的编程语言中,Golang(即Go)作为一门强大且高效的开发语言备受关注。Go
评论:0   参与:  3