Fix Streamlit session_state error: use on_click callback for preset buttons
Browse files
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 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
)
|