1.colab普通用户没有命令行,怎么启动streamlit?

2.如何在colab进行内网穿透,让公网可访问(需要注册ngrok)?

 pip install pyngrok rich

import subprocess
import threading
import sys
from pyngrok import ngrok
from rich import print as rprint
from rich.panel import Panel

from pyngrok import ngrok

#! SET Ngrok Authtoken Here
ngrok.set_auth_token("your authtoken")

def print_output(process):
    for line in iter(process.stdout.readline, ''):
        sys.stdout.write(line)
    for line in iter(process.stderr.readline, ''):
        sys.stderr.write(line)

# Start Streamlit
streamlit_process = subprocess.Popen(
    ["streamlit", "run", "st.py"],
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    universal_newlines=True,
    bufsize=1
)

# Create and start the output printing thread
output_thread = threading.Thread(target=print_output, args=(streamlit_process,))
output_thread.start()

# Create a tunnel using ngrok
public_url = ngrok.connect(8501)
rprint(Panel(f"Streamlit is available at Ngrok ⬇️", expand=False))
print(f"Click 👉 {public_url}")

# Keep the program running
ngrok_process = ngrok.get_ngrok_process()
try:
    streamlit_process.wait()
except KeyboardInterrupt:
    print("Interrupted by user, shutting down...")
finally:
    ngrok.kill()
    streamlit_process.terminate()
    output_thread.join()

》 点击最后输出的 public url 链接即可访问

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐