English 中文(简体)
精简。 案文:投入 Button OnClick for same Input not work better
原标题:Streamlit st.Text_input OnChange and st.Button OnClick for same Input not working correctly

我试图利用用户文本投入,使用户能够打到进入钥匙或利用 but子在 but子下书写文字,然后在 but子被点击或进入使用后澄清投入案文。 当我尝试这一法典时,它将明确案文投入并正确写出案文,但当我使用进入钥匙时,当我使用ton子时,它将写出价值,但明确其价值,并在书写纽伦价值之后,看上去看清这种价值。

import streamlit as st

if "my_text" not in st.session_state:
   st.session_state.my_text = ""

def submit():
    st.session_state.my_text = st.session_state.widget    
    st.session_state.widget = ""

st.text_input("Enter text here", key="widget", on_change=submit)

st.button("Submit", on_click=submit)

my_text = st.session_state.my_text

st.write(my_text)
问题回答

纽伦特语使用纽伦语的行为是由on_change造成的。 页: 1

st.text_input("Enter text here", key="widget", on_change=submit)

www.un.org/Depts/DGACM/index_spanish.htm 例假:

  • User enters "aa" for example
  • Presses the button
  • Executes submit, (**notice this, there was a change in the value from empty to "aa"), and sets st.session_state.widget to empty
  • Executes the rest of the code below
  • It comes back to submit again because the **on_change was triggered.
  • Now the value of st.session_state.my_text is empty

因此,最终没有印本。

如果你尝试这一守则,你可能会发现它如何发挥作用。

import streamlit as st
import logging
import sys

logging.basicConfig(level=logging.INFO, stream=sys.stdout)
logger = st.logger.get_logger(__name__)

logger.info( initial )
if "my_text" not in st.session_state:
    st.session_state.my_text = ""

def submit():
    logger.info( submit %s , st.session_state.widget)
    st.session_state.my_text = st.session_state.widget
    st.session_state.widget = ""

st.text_input("Enter text here", key="widget", on_change=submit)

st.button("Submit", on_click=submit)

my_text = st.session_state.my_text

st.write(my_text)
logger.info( --terminate )

标识如下:

  • first at 11:46:10
  • "text only" at 11:46:29
  • "text and button" (6 lines) at 11:46:49
2024-02-25 11:46:10.359 initial
2024-02-25 11:46:10.479 --terminate
2024-02-25 11:46:29.061 submit text only
2024-02-25 11:46:29.062 initial
2024-02-25 11:46:29.065 --terminate
2024-02-25 11:46:49.201 submit text and button
2024-02-25 11:46:49.201 initial
2024-02-25 11:46:49.205 --terminate
2024-02-25 11:46:49.327 submit
2024-02-25 11:46:49.328 initial
2024-02-25 11:46:49.330 --terminate




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签