Created
May 3, 2022 21:29
-
-
Save asehmi/3c743ae325f04ca2c1327e7a143b5f31 to your computer and use it in GitHub Desktop.
Demonstrate 3 different ways to run operations with form values in Streamlit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import streamlit as st | |
st.header('Experiments with forms') | |
st.markdown('There is no connection between the forms here. The purpose is to demonstrate 3 different ways to run operations with form values.') | |
state = st.session_state | |
if 'word_choices' not in state: | |
state.word_choices = [] | |
en_words = ['streamlit', 'has', 'you', 'covered', 'and', 'just', 'like', 'guinness', 'is', 'good', 'for', 'you'] | |
es_words = ['streamlit', 'tiene', 'usted', 'cubierto', 'y', 'solo', 'como', 'guinness', 'es', 'bueno', 'para', 'usted'] | |
lang = st.radio('Choose language', ['English', 'Spanish']) | |
words = [] | |
if lang == 'English': | |
words = en_words | |
elif lang == 'Spanish': | |
words = es_words | |
word_choices = [] | |
st.write('---') | |
st.markdown('## FORM 1') | |
st.markdown('#### This form simply submits and the external function uses values from the execution scope') | |
with st.form(key='words_form_1', clear_on_submit=True): | |
word_choices = st.multiselect(f'Choose some word options in {lang}', options=words) | |
st.form_submit_button('Apply') | |
def expensive_operation(): | |
st.markdown(f''' | |
### `expensive_operation` on words choices | |
* Assuming this is an expensive operation that should only occur when | |
the word choices are finalized. | |
* The operation is declared AFTER the form, so is called with updated | |
form values after the form is submitted. | |
* If the operation is called INSIDE the form it must be declared BEFORE the form. | |
* This list is held in session state. | |
{word_choices} | |
''') | |
expensive_operation() | |
st.write('---') | |
st.markdown('## FORM 2') | |
st.markdown('#### This form submits and calls the external function directly, in the form itself, with form values') | |
def another_expensive_operation(word_choices): | |
st.markdown(f''' | |
### `another_expensive_operation` on words choices | |
* Assuming this is an expensive operation that should only occur when | |
the word choices are finalized. | |
* The operation is declared BEFORE the form declaration, so is called with updated | |
form values within the form when it is submitted. | |
* If the operation is called OUTSIDE the form it must be declared AFTER the form. | |
* If the operation is declared BEFORE the form, then the form submit must issue a `rerun` | |
after form values have been stored in session state AND the operation should use session | |
state to retrieve these values, OR | |
* This list is held in session state. | |
{word_choices} | |
''') | |
with st.form(key='words_form_2', clear_on_submit=True): | |
word_choices = st.multiselect(f'Choose some word options in {lang}', options=words) | |
if st.form_submit_button('Apply'): | |
another_expensive_operation(word_choices) | |
st.write('---') | |
st.markdown('## FORM 3') | |
st.markdown('#### This form saves and reruns on submit so external functions above and below can run with state values') | |
def yet_another_expensive_operation(): | |
st.markdown(f''' | |
### `yet_another_expensive_operation` on words choices | |
* Assuming this is an expensive operation that should only occur when | |
the word choices are finalized. | |
* The operation is declared BEFORE the form declaration, so is called with updated | |
form values within the form when it is submitted. | |
* If the operation is called OUTSIDE the form it must be declared AFTER the form. | |
* If the operation is declared BEFORE the form, then the form submit must issue a `rerun` | |
after form values have been stored in session state AND the operation should use session | |
state to retrieve these values, OR | |
* This list is held in session state. | |
{state.word_choices} | |
''') | |
yet_another_expensive_operation() | |
with st.form(key='words_form_3', clear_on_submit=True): | |
word_choices = st.multiselect(f'Choose some word options in {lang}', options=words) | |
if st.form_submit_button('Apply'): | |
state.word_choices = word_choices | |
st.experimental_rerun() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment