Hi, I am new to streamlit and working on web visualizer app using 2 buttons functionalities locally, when first button is click displaying pandas data frame which is independent. While second button is click first button part is getting disappeared and app rerun again. But I want both buttons to work independently. I have added dataframe and button to session_state but still not working, So how can I do this?
This is my code:
with st.container():
st.subheader(":rainbow[QUERY PLAYGROUND]", anchor=False)
col1, col2 = st.columns([5, 1], gap="small", vertical_alignment="bottom")
with col1:
question = st.text_input(":orange[Input your question here:]", key="input", placeholder="Type here...")
with col2:
submit = st.button("Get Data", help="Click to submit your question.")
if "df" not in st.session_state:
st.session_state.df = pd.DataFrame()
if "submit_btn" not in st.session_state:
st.session_state["submit_btn"] = False
if "chart_btn" not in st.session_state:
st.session_state["chart_btn"] = False
if submit:
st.session_state["submit_btn"] = not st.session_state["submit_btn"]
if question:
sql_query = get_response(question, prompt)
try:
if sql_query:
st.session_state.df = read_sql_query(sql_query, database_path)
st.subheader(":grey[SQL Query:]", anchor=False)
st.code(sql_query, language='sql')
if not st.session_state.df.empty:
st.subheader(":rainbow[Query Results:]", anchor=False)
st.dataframe(st.session_state.df)
else:
st.write("""<h4 style="color: #ff3333;">No results found for the given query. Try another input...!</h4>""", unsafe_allow_html=True)
except:
st.error("Could not extract SQL query from the response. Please try again to retrieve data or change the input with respect to database.")
st.stop()
else:
st.error("Please enter a valid Question related to database.")
st.stop()
st.subheader(":rainbow[Chart Visualization:]", anchor=False)
col1, col2, col3 = st.columns(3)
with col1:
chart_type = st.selectbox("Select Chart Type", ['Bar Chart', 'Line Chart', 'Pie Chart', 'Scatter Chart'])
with col2:
x_col = st.selectbox("Select X-axis Column", st.session_state.df.columns)
y_col = st.selectbox("Select Y-axis Column", st.session_state.df.columns)
with col3:
generate = st.button("Generate")
if st.session_state["submit_btn"]:
if generate:
st.session_state["chart_btn"] = not st.session_state["chart_btn"]
chart = generate_chart(st.session_state.df, chart_type, x_col, y_col)
st.plotly_chart(chart)