最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Change placeholder based on latest chat input streamlit - Stack Overflow

programmeradmin0浏览0评论

I have this code that supposed to change placeholder dynamically based on latest user input:

def main():
  last_prompt = st.session_state.get(
      "last_prompt", "A cat detective interrogating a suspicious goldfish in a bubble-filled aquarium courtroom.")
  prompt_input = st.chat_input(last_prompt)

  if prompt_input:
    st.session_state.last_prompt = prompt_input

But it's not working as I expected, the placeholder still show default placeholder instead of latest chat input.

I have this code that supposed to change placeholder dynamically based on latest user input:

def main():
  last_prompt = st.session_state.get(
      "last_prompt", "A cat detective interrogating a suspicious goldfish in a bubble-filled aquarium courtroom.")
  prompt_input = st.chat_input(last_prompt)

  if prompt_input:
    st.session_state.last_prompt = prompt_input

But it's not working as I expected, the placeholder still show default placeholder instead of latest chat input.

Share Improve this question asked Mar 2 at 8:23 Muhammad Ikhwan PerwiraMuhammad Ikhwan Perwira 1,1521 gold badge10 silver badges35 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

Using st.rerun solved my problem, but I don't know whether this will be performance issue since I heard there is fragmenting mechanism scope.

def main():
  last_prompt = st.session_state.get(
      "last_prompt", "A cat detective interrogating a suspicious goldfish in a bubble-filled aquarium courtroom.")
  prompt_input = st.chat_input(last_prompt)

  if prompt_input:
    if prompt_input != st.session_state.last_prompt:
      st.session_state.last_prompt = prompt_input
      st.rerun()
  
  # rest of my code...

I believe the problem you encountered is caused by this line of code:

prompt_input = st.chat_input(last_prompt)

last_prompt is only a local variable; it won't be responsible for updating dynamically. You need to use the state variable st.session_state.last_prompt. So the code can be changed as:

prompt_input = st.chat_input(st.session_state.last_prompt)

Besides, as you also have a default value, which is "A cat detective interrogating a suspicious goldfish in a bubble-filled aquarium courtroom." You can have a logic before the state happens

if "last_prompt" not in st.session_state:
        st.session_state.last_prompt = "A cat detective interrogating a suspicious goldfish in a bubble-filled aquarium courtroom."
发布评论

评论列表(0)

  1. 暂无评论