Reuse Snowflake connection throughout Streamlit app #99
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.
Resolves #90, #97
In the current version of the admin app, we manually instantiate a
SnowflakeConnection
multiple times throughout the codebase. This isn't particularly a problem when the user is using standard username/pw credentials; however, when using MFA or external-browser authentication, this causes multiple pings/browser windows to pop up, which interrupts the UX significantly.I've modified the Streamlit app to instantiate a single top-level Snowflake connection and cached it via
st.cache_resource
. With Streamlit's caching, whenever the decorated function is run for the very first time in the lifetime of the app, the connection is created and stored in cache. Whenever the function is invoked again, the connection is retrieved from cache instead of instantiated from scratch. See https://docs.streamlit.io/develop/concepts/architecture/caching#stcache_resource for more details.I also had to make some changes to the underlying model generation/validation logic so that a connection can optionally be passed in instead of instantiating new connections.
Testing
Use the app with a user/account that uses the
externalbrowser
authenticator type. Verify that you see a single auth popup on app start, and only once. Verify that you do not see any more popups while using the app.