Automatically load globals.py
for Express apps
#1172
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #1079.
For Shiny Express apps, this PR makes it so that if there is a
globals.py
file in the same directory as the app file, it will be loaded with the session context set toNone
.Note that even if the user does not have an
import globals
statement in their app file, this will still run the code inglobals.py
. If they want to actually access the variables defined in that file, they will have to an import, as inimport globals
. The part of this that may be surprising is that the code in globals.py could execute even though it's not being referred to anywhere in the app code.@jcheng5 It feels weird to me that
globals.py
would be executed simply because it's in the same directory as the app file, and that behavior could be surprising for users. Like, Imagine thatglobals.py
sets up a database connection and/or writes something to a log file, and the user removes or comments outimport globals
from their app. If it still keeps establishing the database connection and writing to a log file, that would be really surprising and would be difficult to diagnose and debug if you didn't know that how Shiny Express always loads that file.Another way we could get the same effect is to walk the AST and look for
import globals
orfrom globals import ...
and if it finds that, rewrite the AST to:Or, instead of rewriting the AST, during evaluation time we could simply set the session context to
None
before evaluating the import line.With this change, the example app from #1079 works, after renaming
data
toglobals
:Full source code
app.py
globals.py