I have just created my first app.py for Streamlit. However, the lines of code are growing fast, as I am creating a lot of different sections with information.
I want to split the contents of app.py into subfiles.
I tried using exec(open("./subfile.py").read()), which works, but my understanding is that this is bad practice. (NB: I’m not importing any external code in the subfiles, only creating some variables and referencing some external iframe URLs via components.html and components.iframe.
Furthermore, Streamlit doesn’t detect changes in subfile.py so you aren’t prompted to Rerun or Always Rerun the code upon editing it.
How should I split my project into multiple files, and can it be made to work with Always Rerun?
You are right, moving things to multiple files can be tricky, because Streamlit doesn’t necessarily monitor every folder that might have Python code in it. The way I’d suggest breaking things up would be similar to structuring other Python code. Where possible, writing functions is a great idea, and as functions get finalized, you can move them to other files and not be worried about refreshes.
In terms of exec, yes, you almost never want to execute raw strings in any programming language unless you are 100% sure of their contents. In Python, you almost certainly shouldn’t be doing it.
Is there a code repo you are willing to share that we can give you tips about?
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.