Hi, Iām using streamlit-ace to configure a json editor. The issue here that Iām creating tabs using a button, and in one of the tabs Iām creating this editor. But as soon as I make any change to editor, the whole tab structure collapses.
Steps to reproduce
Code snippet:
import streamlit as st
from streamlit_ace import st_ace
import json
button = st.button("press")
if button:
tabs = st.tabs(["1"])
with tabs[0]:
data = {"a" : "B"}
content = st_ace(value=json.dumps(data, indent=2), language="json", theme="clouds_midnight", auto_update=True)
content
b = st.button("check result")
if b :
st.json(content)
st.write(json.dumps(content, indent=2))
If applicable, please provide the steps we should take to reproduce the error or specified behavior.
Expected behavior:
I should be able to make changes in the editor and use those changes, but since everything is collapsing I canāt do that.
Actual behavior:
There are no errors. Itās just that since weāre producing tabs with a button, thatās why itās behaving this way. If we were doing this without buttons, it would work flawlessly. But in my application, I need the run button.
PS - Any workaround would be appreciated. I tried dividing the tabs part into separate pages, so that its dependency on button can be eliminated, but imports are impossible.
@edsaac@blackary Thanks for you inputs, but I am still facing an issue Iām not able to crack.
I am receiving data from database in one button called, letās say, āFetch Dataā. Now I want to use this data (dataframe obj) in another button āShow Resultsā.
This is what the code for it looks like:
if st.button("Fetch Data"):
df = run_profiler(database=database, schema=schema, table=table, sampling_percent=sampling_percent)
st.write("Evaluation complete. Click on Show Results to view the Profiling results.")
show_results = button("Show Results", key="show_results")
if show_results:
st.dataframe(df)
show_profiling_results(df)
Iām able to fetch the data in df, but not able to use that df variable outside the scope of the first if condition. I tried with both buttons (as mentioned above). What am I doing wrong here?
Preferably, I want the āShow Resultsā button to show up after the āFetch Dataā button is clicked, but if thatās not possible, I can make this work too!
NOTE: I saw that one of the solutions is to use checkbox instead of button, but in that case, when I click on āShow Resultsā, it goes back to the checkbox and runs the fetch query again (twice??), which will cause it to be very, very slow. And every time I make any change to the editor placed inside āShow resultsā it runs the fetch query.
Thanks in advance!
Hi, I have found a way to use session_state to pass data between buttons, and to also avoid rerun of the entire app. Here is the code:
# Initialize Button State
if 'fetch_data_state' not in st.session_state:
st.session_state.fetch_data_state = False
if not st.session_state.fetch_data_state:
if st.button("Fetch Data"):
st.session_state.fetch_data_state = True
if 'df_state' not in st.session_state:
st.session_state.df_state = None
df = fetch_data(database=database, schema=schema, table=table,sampling_percent=sampling_percent)
st.session_state.df_state = df
if st.session_state.fetch_data_state:
show_results = button("Show Results", key="show_results")
if show_results:
st.dataframe(st.session_state.df_state)
NOTE: The Show Results button is streamlit-extras button (to save state). Hope this helps!