I started using the multi pages feature to create a streamlit app, so far its been nice. However I have encountered a sort of bug (Although I understand this is a expected behavior, because of my approach) . I am loading a custom css file to change the font of the app, I am doing this in every page .py file, now while it works, every time I switch between pages it takes a fraction of a second for the page to load the custom css and its quite noticeable and annoying.
I tried wrapping the code to load the css file with a st.cache decorator, but was greeted with a CachedStFunctionWarning.
This is the code to load the css file.
with open(file_name) as f:
st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
Ideally I would want the css to load instantly on all pages upon app launch without it refreshing every time i switch to a different page, and being able to configure it globally would also be nice.
Are there any ways of accomplishing this? Could we have the option to include custom fonts on the config.toml file instead of just the current available ones?
Have you tried playing directly with Streamlit theming?
I don’t know how you wrote in the caching attempt, so if you could share how you structured it as a function that might shed some light. Also, have you tried the new st.experimental_memo? There can be some fiddly bits with caching, but if you cache the actual css to inject as a string to load on the page, I don’t think there should be a problem.
If you are really determined to get this done you could try injecting the custom stylesheet into the base HTML page of the installed Streamlit package. I would be hesitant to suggest a specific implementation but you can look here for inspiration. In one of my apps I adapted some of this code to be run at Docker image build time. Or just use that package’s API but try to understand what it does first and that it could break easily with changes in Streamlit.
Hello, Is there any possible solution for this now?
I have a much similar problem. When I switch pages css stylings takes a fraction of second to be apply. I would like to prevent this.
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.