Running time in streamlit

Hello to you!,
I am an user of streamlit however although inserting @ st.cache commands to my code, the running time is always extremely long which is a disadvantage for my presentations because although people are captivated by the tool , the running time which is not instantaneous on heavier projects discourages more than one.
So I would like to know if it is possible to just run a specific part of the code on which we have made modifications (for example when changing some parameters on the side board) without running all the code as is the case now.
Thank you for your reply.

Hi @emerys693, welcome to the Streamlit forum!

Without seeing the code, using st.cache is the expected way to handle these things. Essentially, the programmer is declaring which steps need to be constantly re-calculated vs. can remain static. Are there steps in your app that can’t be cached and take a long time?

Best,
Randy

Hey @emerys693,

To answer your questions (as continued from the email you sent me), I have a few suggestions for your code:

  • You load in about 30 or so trained and pickled ML’s, I imagine that these take a lot of time to do. The best thing for these, since you wouldn’t expect to change their training, is to put them in a function and use the @st.cache decorator on them.

  • Do you always need to load all the ML’s in? It seems there are a variety of pre-trained models that work for different machine learning types. Maybe when the user makes selections on the different widgets, you can load just the relevant ML’s for that case (ie. only load the regression models when someone selects regression)

  • You have a bunch of selectbox, slider and number_input widgets. This can slow things down as you have them coded because by changing any of those widgets the script re-runs. That can easily be solved by our new st.forms widget that will be able to group these together so the user can set all of the widgets and have the script only run once when they click the st.form_submit_button checkout the docs here: Cookbook — Streamlit 0.81.1 documentation

  • Note: I’m not 100% sure what your main_app.py file is but I was looking at the couverture_B0.py file as that seems to be where you import Streamlit and create the title, widgets and main app layout

@randyzwitch here is the link to the GitHub repo, let us know if you see any other tips or tricks they can use to speed up their app!

*Happy Streamlit-ing! *
Marisa

1 Like

hi !
thanks for your reply.
the good main.py is couverture_B.py . i look at the document for st.form but i don’t really nderstand how it work.
cordially.

Marisa_Smith
May 11

Hey @emerys693,

To answer your questions (as continued from the email you sent me), I have a few suggestions for your code:

  • You load in about 30 or so trained and pickled ML’s, I imagine that these take a lot of time to do. The best thing for these, since you wouldn’t expect to change their training, is to put them in a function and use the @st.cache decorator on them.

  • Do you always need to load all the ML’s in? It seems there are a variety of pre-trained models that work for different machine learning types. Maybe when the user makes selections on the different widgets, you can load just the relevant ML’s for that case (ie. only load the regression models when someone selects regression)

  • You have a bunch of selectbox, slider and number_input widgets. This can slow things down as you have them coded because by changing any of those widgets the script re-runs. That can easily be solved by our new st.forms widget that will be able to group these together so the user can set all of the widgets and have the script only run once when they click the st.form_submit_button checkout the docs here: Cookbook — Streamlit 0.81.1 documentation

  • Note: I’m not 100% sure what your main_app.py file is but I was looking at the couverture_B0.py file as that seems to be where you import Streamlit and create the title, widgets and main app layout

@randyzwitch here is the link to the GitHub repo, let us know if you see any other tips or tricks they can use to speed up their app!

*Happy Streamlit-ing! *
Marisa

hi!
I’m not 100% sure what your main_app.py file is but I was looking at the couverture_B0.py file as that seems to be where you import Streamlit and create the title, widgets and main app layout : the good one is couverture_B.py.

  • You load in about 30 or so trained and pickled ML’s, I imagine that these take a lot of time to do. The best thing for these, since you wouldn’t expect to change their training, is to put them in a function and use the @st.cache decorator on them.: i use the cache where i suppose it can be use for the rest can you suggest or show me a way to build a function? for example it there a way to build a function for the interaction i insert? or something else?

  • You have a bunch of selectbox, slider and number_input widgets. This can slow things down as you have them coded because by changing any of those widgets the script re-runs. That can easily be solved by our new st.forms widget that will be able to group these together so the user can set all of the widgets and have the script only run once when they click the st.form_submit_button checkout the docs here: Cookbook — Streamlit 0.81.1 documentation: i read the document thanks for that but i’m still confuse on how to adapt it on my case i want to keep yhe board on the left side of my screen for an easy acces but i see in the exampe that it will be on the top of the screen furthermore, i tried to rerun the example in the docume and obtain this

    AttributeError: module 'streamlit' has no attribute 'form'
    

    i update streamlit but still get this error.

  • can i have a simple case of how to code this st.forms base on my code so that i can use it

cordially.

Marisa_Smith
May 11

Hey @emerys693,

To answer your questions (as continued from the email you sent me), I have a few suggestions for your code:

  • You load in about 30 or so trained and pickled ML’s, I imagine that these take a lot of time to do. The best thing for these, since you wouldn’t expect to change their training, is to put them in a function and use the @st.cache decorator on them.

  • Do you always need to load all the ML’s in? It seems there are a variety of pre-trained models that work for different machine learning types. Maybe when the user makes selections on the different widgets, you can load just the relevant ML’s for that case (ie. only load the regression models when someone selects regression)

  • You have a bunch of selectbox, slider and number_input widgets. This can slow things down as you have them coded because by changing any of those widgets the script re-runs. That can easily be solved by our new st.forms widget that will be able to group these together so the user can set all of the widgets and have the script only run once when they click the st.form_submit_button checkout the docs here: Cookbook — Streamlit 0.81.1 documentation

  • Note: I’m not 100% sure what your main_app.py file is but I was looking at the couverture_B0.py file as that seems to be where you import Streamlit and create the title, widgets and main app layout

@randyzwitch here is the link to the GitHub repo, let us know if you see any other tips or tricks they can use to speed up their app!

*Happy Streamlit-ing! *
Marisa

Hey @emerys693,

What version of Streamlit did you upgrade to? can you run streamlit --version in a terminal and let me know what the result is?

Here is a minimal example:

# lets create a form and put widgets in it

#first we let streamlit know that we will be making a form
my_form = st.form(key="test_form")

# next lets add a set of widgets to our form,
# all of these widgets will not trigger a rerun
# of my code until the submit button is pressed
name = my_form.text_input("Your name")
age = my_form.number_input("Your age", value = 20)
first_time = my_form.checkbox("check if this is your first time using forms")
number = my_form.slider("pick a number", value=0)

#all forms end with a submit button, that is how the user can trigger
# a rerun of the app with the setting they chose!
submit = my_form.form_submit_button(label="submit")

# now this part only runs once someone is finished filling in
# the form and they want to see the output of the app
if submit:
    st.write("Welcome %s" % name)
    st.write("You are %i years old" % age)
    st.write("Your chosen number is: %i" %number)
    if first_time:
        st.write("well done on your first form")
    else:
        st.write("I see you are an expert at these!")

Here is what that looks like in an app:

Happy Streamlit-ing!
Marisa