Since the acquisition of Streamlit last year, significant steps have been taken in terms of integration with the overall Snowflake platform. This has recently culminated in the ability to create, run, and debug Streamlit Apps from within Snowflake using the Snowsight interface.
This short blog will provide a quick optimization tip in relation to using Streamlit. But first things first:
What is Streamlit?
Streamlit is an open-source Python library for creating and sharing custom web apps. Use case examples : machine learning, data science. For those who wish to get hands-on experience, there are plenty of tutorials and coding examples here.
Streamlit optimization tip: Caching
A key consideration when building Streamlit apps is caching, particularly because Streamlit re-runs the Python script with each user interaction. Therefore it’s important to be aware of functions such as:
- cache_data — as the name suggests, for caching data e.g. loading a DataFrame from CSV, or data from the Snowflake Marketplace
- cache_resource — for caching e.g. ML models, database connections
These are actually Python “decorators” which act as wrappers for other functions such as the load_data example in the screenshot.
Bonus tip: ttl
The eagle eyed among us will notice, in the screenshot above, a parameter being used in the cache_data function called ttl.
This stands for “time to live”, and represents the maximum time in seconds within which to keep an entry in cache. Ttl is configured depending on how often the cache refreshes which will vary depending on use case.
Conclusion
Streamlit apps can now be designed, run, and debugged through the Snowflake Snowsight interface. Please note that this configuration is in public preview, and is currently only available on Snowflake AWS (non-government) with more regions to follow.
Most importantly, Streamlit by default re-runs the Python script with each user interaction. So if you are developing Streamlit Apps e.g. on Snowflake and require performance optimization, do keep cache_data and cache_resource in mind!
© Dan Galavan 2023