My goal is to deploy a web app (originally in Jupyter Notebooks) that generates many complex matplotlib figures. It would take very long to reformat them all into Plotly. From reading the docs and blog posts, it seems like matplotlib will struggle with concurrent users. Currently I’m running it locally, but want to future-proof. The streamlit docs recommend implementing matplotlib with the RendererAgg backend as follows:
# Imports
import streamlit as st
import matplotlib as mp
import matplotlib.pyplot as plt
# # Change matplotlib backend
from matplotlib.backends.backend_agg import RendererAgg
_lock = RendererAgg.lock
# # Generate figure
with _lock:
fig, axs = plt.subplots(figsize = (5, 5))
plt.plot([1,2], [0, 0])
# # Render in streamlit
st.pyplot(fig)
This gives me the following error: AttributeError: type object ‘RendererAgg’ has no attribute ‘lock’
AttributeError: type object 'RendererAgg' has no attribute 'lock'
Traceback:
File "C:\...\streamlitenv\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 542, in _run_script
exec(code, module.__dict__)File "D:\...\scrap.py", line 17, in <module>
_lock = RendererAgg.lock
^^^^^^^^^^^^^^^^
If I remove the lines calling _lock (_lock = RendererAgg.lock
and with _lock:
), my local file displays the plot normally. What is going on with RendererAgg? In my streamlitenv environment, I am running matplotlib 3.8.0, streamlit 1.32.0, and python 3.11.7.
EDIT: fixed formatting