We notice there is interference while using standard libraries across sessions. We noticed this in the random and matplotlib libraries in our project.
We reproduce this error with this snippet of code :
Please run two sessions (open two windows or browsers with the same app) and click on start such that both apps run simultaneously.
import streamlit as st
import random
button1 = st.button("Start")
if button1:
random.seed(1)
for i in range(100000000):
random.random()
st.write(random.random())
When we run them independently, the apps run as expected. But, when there is overlap or while the app is run on both sessions simultaneously, there is interference. This can be seen in the changed value of the write. Even Matplotlib library has a similar issue while trying to plot.
This is a workaround for the code discussed above such that even with overlapping computation or simultaneous run, the returned answer is identical.
import streamlit as st
import random
button1 = st.button("Start")
if button1:
i=0
for i in range(500000):
random.seed(i)
random.random()
random.seed(i)
st.write(random.random())
This workaround involves constant re-setting of the seed value. Please advise.
An example for the interference with the Matplotlib library is as follows :
With two sessions opened, click on button 2 on one of the sessions and immediately click on button 1 in the other session.
import matplotlib.pyplot as plt
import streamlit as st
import time
one = st.button("1")
two = st.button("2")
if one:
x = [0,1,2,3,4]
y = x
plt.title("Hello 1")
plt.plot(x,y)
st.pyplot(plt)
if two:
plt.title("Hello 2")
x = [0,1,2,3,4]
y = x
plt.plot(x,y)
plt.title("Hello 1")
time.sleep(5)
x = [4,3,2,1,0]
plt.plot(x,y)
st.pyplot(plt)
Notice that while both buttons are pressed with overlap, the plot in button 2 changes.
Quite interesting. Streamlit sessions run in different threads, and matplotlib seems to use the same plot stack across them. And in your example, the plot Hello 1 actually displays two overlapping lines.
Some solutions exist, for example this one which recommends to use subplots:
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.