A few use cases we have in mind for Streamlit encourage writing of code:
Write a regex, visualize selection of gradients/activations that correspond to the rules
Customize viz code inline to change the way data is presented
Is there any way to embed or execute a Jupyter cell inside of a streamlit app? Obviously I could use an iframe, but love to have a cell that exists within the broader context of the app.
There’s no way to embed a Jupyter cell in Streamlit but you could emulate that using text areas:
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt
'''
Try changing the code in the text area below to
`plt.plot(data[:, 1])`
'''
data = np.random.randn(100, 2)
default_code = 'plt.plot(data)'
code = st.text_area('Enter some code', default_code)
exec(code, locals())
st.pyplot()
Of course, you should only let trusted users use this script! You’re allowing people to run arbitrary Python code, which means they can cause a lot of damage on your machine.
If you want to do live Jupyter-style exploratory analysis, I find that with my text editor on the left side of the screen and my browser on the right, I can use Streamlit’s automatic rerun feature to update the browser every time I press ctrl-s on my editor (to save the script). This gives me a super fast iterative loop, that feels great.
You may need to sprinkle some st.cache here and there as the need arises, but the nice thing is that at the end of your fun exploratory coding session you end up with a really nice Streamlit app.
Alternatively, if what you’re looking for is some way to allow your viewers to edit some part of your code, some time ago I wrapped the snippet above into a slightly more usable form and published as a Streamlit component called execbox, which may be useful for that.
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.