Hi, first of all shout out to the streamlit team for this awesome piece of software. I am new to streamlit and I was wondering if there is a way to create a form with streamlit? In other words, the script is only rerun on a submit button, whereas other widgets are specified not to rerun the script even when their states are changed.
Hey @gunabam,
Welcome to our Streamlit community!!!
Thanks for the shout out! We have a pretty great engineering team that never ceases to amaze (me at least! )
You can definitely create a form in Streamlit, I was able to create a simple one that only displays or does something with the values that the person inputs once the final date value is changed from its initial start date. Here the Script is still rerun each time you input a value, but the key is that you don’t do anything with those values until the final value is filled in. I think in most cases this could work!
import streamlit as st
import datetime
#make a title for your webapp
st.title("An Input Form")
#lets try a both a text input and area as well as a date
field_1 = st.text_input('Your Name')
field_2 = st.text_area("Your address")
start_date = datetime.date(1990, 7, 6)
date = st.date_input('Your birthday', start_date)
if date != start_date:
field_1
field_2
date
However, if you are set on trying to create something based on states, there is a link here to the session_state where you may be able to work on a more complex solution!
Multi-page app with session state
Happy Streamlit-ing!
Marisa
Hi @Marisa_Smith,
This is a simplified version of the code that I am trying to make. I have a button that loads a bunch of different large tables and plots. Then I have a bunch of filters that remove rows in the table or points on a graph. To avoid rendering massive tables or graphs everytime a state of a widget changes, I have a filter
button that applies all the filters at once. The thing is the initial loaded tables or graphs disappear if i use the sliders (since the script reruns and the load button is not clicked). Is there away to not rerun the script for the sliders, or a way to prevent large tables from being rendered everytime (like I have cached these large tables or the data for Plotly plots but its just matter of repeatedly rendering)?
import streamlit as st
import pandas as pd
st.title('Test Data')
# data
sample_data = [{
"C1": 1,
"C2": 5,
"C3": [],
"C4": True
}, {
"C1": 2,
"C2": 6,
"C3": ['1', '2'],
"C4": False
}, {
"C1": 3,
"C2": 7,
"C3": ['2'],
"C4": True
}]
sample_df = pd.DataFrame(sample_data)
# widgets
table = st.empty()
load_button = st.button('Load')
filter_1 = st.slider('C1 filter',
min_value=1,
max_value=3,
value=1,
step=1)
filter_2 = st.slider('C2 filter',
min_value=5,
max_value=7,
value=5,
step=1)
filter_button = st.button('Filter')
# actions
if load_button:
table.dataframe(sample_df)
if filter_button:
sample_df = sample_df[(sample_df.C1 >= filter_1) & \
(sample_df.C2 >= filter_2)]
table.dataframe(sample_df)
Will streamlit caching meet your need?
You use it on your data loading/generation function and the tables will be preserved across connected sessions.
@theimposingdwarf this may solve the problem, @gunabam let us know if this worked and I can mark it as a solution!
I have another idea that might work! It seems that you need the load_button
pre-selected. Now the st.button
function has no functuonality currently to do this but the st.checkbox
does! You can pass the parameter value=True
and it will check the checkbox on running of the script:
import streamlit as st
st.title('Generate a checkbox that is already checked on script run')
load_check = st.checkbox('Load', value=True)
gives:
This will allow your script to re-run with the filters but not reset the load_button
to False!
Happy Streamlit-ing!
Marisa
Thank you @theimposingdwarf and @Marisa_Smith - it worked!