[英]on_click function assigned to Streamlit button runs before the click event in browser
I am trying to run a script in streamlit button with onclick=""
parameter.我正在尝试使用
onclick=""
参数在 streamlit 按钮中运行脚本。 Problem is, the script start running before even button shows up on the browser.问题是,脚本甚至在按钮出现在浏览器上之前就开始运行。 Any idea what maybe the mistake here?
知道这里可能是什么错误吗?
import streamlit as st
from level_packs import create_packs
st.set_page_config(page_title="Main work", page_icon="💻")
st.markdown("# Main work")
st.button('Run script', on_click=create_packs())
I tried with below code and its working:)我尝试使用以下代码及其工作:)
import streamlit as st
from level_packs import create_packs as f
st.set_page_config(page_title="Main work", page_icon="💻")
st.markdown("# Main work")
def run_button():
st.button('Run script', on_click=f)
run_button()
You could initialize a session state for the button and pass the function to an if
statement.您可以为按钮初始化 session state 并将 function 传递给
if
语句。
Something like:就像是:
runbtn = st.button('Run script')
if "runbtn_state" not in st.session_state:
st.session_state.runbtn_state = False
if runbtn or st.session_state.runbtn_state:
st.session_state.runbtn_state = True
create_packs() # Your function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.