zaid646 commited on
Commit
e63fc5a
·
1 Parent(s): b1b3b94

Fix Streamlit session_state error: use on_click callback for preset buttons

Browse files
Files changed (1) hide show
  1. app.py +10 -3
app.py CHANGED
@@ -96,6 +96,9 @@ PRESET_PROMPTS = [
96
  "Plot a sine wave and a cosine wave on the same chart using matplotlib, add a legend, and save it to /tmp/waves.png.",
97
  ]
98
 
 
 
 
99
  for i, p in enumerate(PRESET_PROMPTS):
100
  is_selected = st.session_state.get("prompt_input") == p
101
  border = "1px solid #6366f1" if is_selected else "1px solid #e2e8f0"
@@ -107,6 +110,10 @@ for i, p in enumerate(PRESET_PROMPTS):
107
  )
108
  use_col1, use_col2, _ = st.columns([1, 1, 4])
109
  with use_col1:
110
- if st.button("Use this prompt", key=f"use_prompt_{i}", use_container_width=True):
111
- st.session_state.prompt_input = p
112
- st.rerun()
 
 
 
 
 
96
  "Plot a sine wave and a cosine wave on the same chart using matplotlib, add a legend, and save it to /tmp/waves.png.",
97
  ]
98
 
99
+ def _set_prompt(value: str):
100
+ st.session_state.prompt_input = value
101
+
102
  for i, p in enumerate(PRESET_PROMPTS):
103
  is_selected = st.session_state.get("prompt_input") == p
104
  border = "1px solid #6366f1" if is_selected else "1px solid #e2e8f0"
 
110
  )
111
  use_col1, use_col2, _ = st.columns([1, 1, 4])
112
  with use_col1:
113
+ st.button(
114
+ "Use this prompt",
115
+ key=f"use_prompt_{i}",
116
+ use_container_width=True,
117
+ on_click=_set_prompt,
118
+ args=(p,),
119
+ )