What are some examples of programming models that are similar to Streamlit?
My current strategy has been to store āviewā controls in a separate module, and then just calling view.put_button_here() while behind the scenes Iām doing state.control = st.button(), and from anywhere else I call state.control whenever I need that state, and then I springle some old school layering on top, like a Repository class to clean up the data access.
Besides that I have really no idea what an ideal architecture should look like.
I donāt have much experience outside of Python. Would SwiftUI be a similar analogy to follow? Streamlit projects Iāve looked at are mostly teeming with ābackend logicā, and really only have a slider and a graph on the frontend, so they donāt really have to deal with any of these issues.
I mean thatās sort of the whole point of Streamlit, isnāt it? Instead of having to implement design patterns like MVC within complex frameworks, you can just sprinkle a few Streamlit calls throughout your script and youāve got an app. Thatās the power.
However, I do think your question is interesting because I have also found myself asking, āwhere does it end?ā How do I strike a balance between preserving the simplicity of what Streamlit was intended to accomplish, and not ending up with poorly-designed code? As much as I want to embrace the āStreamlit wayā, if youāre working on a larger project itās very easy to end up with a 500 line script, and thatās just not good practice.
What Iāve been trying out in the app Iām working on (code here), is somewhere in-between a single script and full-blown MVC: a separation of concerns by use of modules.
main.py
The script which is run. All Streamlit calls here. Acts a bit like view and controller combined. This is where the app is put together.
data_processor.py
All pandas stuff in here. Data cleaning and manipulation, building DataFrames etc.
data_scraper.py
API calls and data scraping done here. Async functions for speedier API calls in here also.
plotter.py
I style my plots here with plotly express. Not necessary if youāre not going to do any fancy plot styles.
For the most part, the latter three donāt talk to each other, except trough main.py.
I may add a text.py module too, to store long Strings that I write to the app in main.py.
Iām still getting a feel for it, but I think the question is pertinent. Iām worried that an unintended side effect of this paradigm could be poor code design.
If I work out a more general framework/structure for larger apps Iāll definitely share it!
I mean thatās sort of the whole point of Streamlit, isnāt it? Instead of having to implement design patterns like MVC within complex frameworks, you can just sprinkle a few Streamlit calls throughout your script and youāve got an app. Thatās the power.
True true. But itās also becoming a framework of its own as more features are added. Itās fast and tempting to create more and more complex applications, but like any project they end up becoming pretty messy without a more stringent measures
For example, I like the separation of conerns in your example, but dislike how state must be massaged and managed around by the data_processor. It quickly blows up if you have e.g. several checkboxes and conditionals to manage.
I guess one could study some of the state-management philosophies associated with React, since it feels quite similar to Streamlit (without actually having used it, ever)
Good point, although I havenāt run into any difficulties yet. Think of data_processor.py - and all files except main.py - as a helper function module.
Any checkboxes and/or conditionals will be managed exclusively in main.py.
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.