Hi, I want to be able to test various functions I use in my streamlit app through channels like ipython or pytest. However when I run functions that have the @st.cache() decorator they break. So is there a nice way to check if streamlit is running and disable those functions?
Ideally there should be a way to ignore streamlit calls when they do not make sense so that you can run the script in another environment without modification.
For the caching problem I’m thinking something like this:
def cache():
"""Wrapper to disable streamlit caching when running with ipython
"""
def wrapper(function):
if st.running:
wrapped = st.cache(persist=True)(function)
else:
wrapped = function
return wrapped
return wrapper
Currently the answer to me is to split the functions into two.
I think I would start by implementing the general functions in seperate files, modules and or packages. They are of general use and should be implemented as such. I can then work with them and test them in the usual way.
Then in my Streamlit app I wrap those functions with the @st.cache annotation.
from my_module import my_func
@st.cache
def my_func_cached(x):
return my_func(x)
I would not do the above for small apps and/ or when I start out developing. But as my app and functionality grows, I believe I would then refactor to the above.
Comments and suggestions to this is really appreciated. Maybe I should start writing something about my understanding of Streamlit Architecture Best Practices at https://awesome-streamlit.readthedocs.io/en/latest/. Then hopefully I could get some feedback and improve.
You can ignore all calls that print to a Streamlit app by setting this config option:
st.set_config('client.displayEnabled', False)
But this doesn’t impact st.cache. Whether this is the desired behavior, it depends on your use-case. We’re open to changing this behavior / adding a separate config option to address other use-cases, but we’d need to understand the reasoning a bit more first.
If you just need to skip the cache for testing purposes, the preferred solution is to patch st.cache like this:
from unittest.mock import patch
def mock_cache(func=None, *args, **kwargs):
if func is None:
return lambda f: f
return func
@patch('streamlit.cache)
def test(cache_mock):
cache_mock.side_effect = mock_cache
# The rest of the test goes here.
(Note: I haven’t tested this code!)
As for the ipython use-case, I think it depends on what you want exactly. One hack would be to just overwrite st.cache when in a REPL:
import streamlit as st
if st._util.is_repl():
st.cache = mock_cache # see mock_cache definition above
There are other possible solutions too, such as checking whether you’re running in a Streamlit thread first, with:
import streamlit.ReportThread as ReportThread
is_streamlit_thread = ReportThread.get_report_ctx()
if is_streamlit_thread:
# overwrite st.cache here as before...
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.