简体   繁体   English

如何使用 Streamlit 在单击按钮时切换页面?

[英]How to switch page on button click using Streamlit?

I have made separate functions for each page, but i want to change page to file upload when I click button on welcome_page.py.我为每个页面制作了单独的功能,但我想在单击 welcome_page.py 上的按钮时将页面更改为文件上传。 Found a switch_page function but I don't think I understand how it works.找到了 switch_page function 但我不明白它是如何工作的。

import streamlit as st
from login import check_password
from file_upload import file_upload
from welcome import welcome_page
from PIL import Image
from numpy import asarray
from streamlit_extras.switch_page_button import switch_page

def file_upload():
    datafile = st.file_uploader("Upload JPG",type=['jpg'])
    if datafile is not None:
        numpydata = asarray(datafile)
        print(type(numpydata))
        st.image(datafile)
        return True
    
    return False

if check_password():
    if welcome_page():
        switch_page('file_upload')
        fileDetails = file_upload()
        if fileDetails:
            print("file uploaded")

Unfortunately, there isn't a built-in way to switch pages programmatically in a Streamlit multipage app.不幸的是,在 Streamlit 多页面应用程序中没有内置的方式来以编程方式切换页面。 There is an open feature request here if you'd like to upvote it.如果您想投票,请在此处提出开放功能请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM