title: Save matplotlib chart from Dash/Flask
body: <p>I've a Dash app running which calls functions which create and save charts.<br />
  The app runs into timeouts which are caused from the drawing of the charts.
  Matplotlib prints the warning:<br />
  <code>Starting a Matplotlib GUI outside of the main thread will likely fail.</code></p>
  <p>From my unterstanding the Dash app is hosted by Flask which runs different threads. This seems to be a problem for matploblib since it's not thread save. I run the app with the <code>threaded=False</code> parameter but the problem still exists. When debugging the app it seems like Flask is still running multiple threads.</p>
  <p>The proposed solution from the matplotlib <a href="https://matplotlib.org/stable/gallery/user_interfaces/web_application_server_sgskip.html" rel="nofollow noreferrer">website</a> is not to use pyplot but instead use the OOP approach.</p>
  <p>Does someone know another solution to solve the problem?</p>
  <p>Example code:</p>
  <pre><code>import dash
  from dash import html
  from dash.dependencies import Output, Input
  import matplotlib.pyplot as plt
  
  app = dash.Dash(__name__)
  app.layout = html.Div([html.Button(&quot;Button&quot;, id=&quot;btn&quot;), html.Div(id=&quot;placeholder&quot;)])
  
  
  def draw_figure():
      fig, ax = plt.subplots(1, 1)
  
  
  @app.callback(
      Output(&quot;placeholder&quot;, &quot;children&quot;),
      Input(&quot;btn&quot;, &quot;n_clicks&quot;),
      prevent_initial_call=True,
  )
  def func(_):
      for _ in range(20):
          draw_figure()
      return &quot;&quot;
  
  
  if __name__ == &quot;__main__&quot;:
      app.run_server(debug=True, threaded=False)
  </code></pre>