8000 Turn "How to use Matplotlib in a web application server" into a sphinx-gallery example by Transfusion · Pull Request #18767 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Turn "How to use Matplotlib in a web application server" into a sphinx-gallery example #18767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 0 additions & 52 deletions doc/faq/howto_faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -526,55 +526,3 @@ artists.
You may be able to work on separate figures from separate threads. However,
you must in that case use a *non-interactive backend* (typically Agg), because
most GUI backends *require* being run from the main thread as well.

.. _howto-webapp:

How to use Matplotlib in a web application server
=================================================

In general, the simplest solution when using Matplotlib in a web server is
to completely avoid using pyplot (pyplot maintains references to the opened
figures to make `~.matplotlib.pyplot.show` work, but this will cause memory
leaks unless the figures are properly closed). Since Matplotlib 3.1, one
can directly create figures using the `.Figure` constructor and save them to
in-memory buffers. The following example uses Flask_, but other frameworks
work similarly::

import base64
from io import BytesIO

from flask import Flask
from matplotlib.figure import Figure

app = Flask(__name__)

@app.route("/")
def hello():
# Generate the figure **without using pyplot**.
fig = Figure()
ax = fig.subplots()
ax.plot([1, 2])
# Save it to a temporary buffer.
buf = BytesIO()
fig.savefig(buf, format="png")
# Embed the result in the html output.
data = base64.b64encode(buf.getbuffer()).decode("ascii")
return f"<img src='data:image/png;base64,{data}'/>"

.. _Flask: http://flask.pocoo.org/

When using Matplotlib versions older than 3.1, it is necessary to explicitly
instantiate an Agg canvas; see e.g. :doc:`/gallery/user_interfaces/canvasagg`.


.. _howto-click-maps:

Clickable images for HTML
-------------------------

Andrew Dalke of `Dalke Scientific <http://www.dalkescientific.com>`_
has written a nice `article
<http://www.dalkescientific.com/writings/diary/archive/2005/04/24/interactive_html.html>`_
on how to make html click maps with Matplotlib agg PNGs. We would
also like to add this functionality to SVG. If you are interested in
contributing to these efforts that would be great.
75 changes: 75 additions & 0 deletions examples/user_interfaces/web_application_server_sgskip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""
.. _howto-webapp:
=================================================
How to use Matplotlib in a web application server
< 8000 span class=pl-s>=================================================
In general, the simplest solution when using Matplotlib in a web server is
to completely avoid using pyplot (pyplot maintains references to the opened
figures to make `~.matplotlib.pyplot.show` work, but this will cause memory
leaks unless the figures are properly closed). Since Matplotlib 3.1, one
can directly create figures using the `.Figure` constructor and save them to
in-memory buffers. The following example uses Flask_, but other frameworks
work similarly:
.. _Flask: http://flask.pocoo.org/
"""

import base64
from io import BytesIO

from flask import Flask
from matplotlib.figure import Figure

app = Flask(__name__)


@app.route("/")
def hello():
# Generate the figure **without using pyplot**.
fig = Figure()
ax = fig.subplots()
ax.plot([1, 2])
# Save it to a temporary buffer.
buf = BytesIO()
fig.savefig(buf, format="png")
# Embed the result in the html output.
data = base64.b64encode(buf.getbuffer()).decode("ascii")
return f"<img src='data:image/png;base64,{data}'/>"

# %%
# When using Matplotlib versions older than 3.1, it is necessary to explicitly
# instantiate an Agg canvas;
# see e.g. :doc:`/gallery/user_interfaces/canvasagg`.
#
# Note: This script should be run using the
# `flask command-line tool <https://flask.palletsprojects.com/en/master/cli/>`_
# since it is a Flask application.
# Assuming that the working directory contains this script:
#
# Unix-like systems
#
# .. code-block:: console
#
# FLASK_APP=web_application_server_sgskip flask run
#
# Windows
#
# .. code-block:: console
#
< 590E /td> # set FLASK_APP=web_application_server_sgskip
# flask run
#
# .. _howto-click-maps:
#
# Clickable images for HTML
# -------------------------
#
# Andrew Dalke of `Dalke Scientific <http://www.dalkescientific.com>`_
# has written a nice `article
# <http://www.dalkescientific.com/writings/diary/archive/2005/04/24/interactive_html.html>`_
# on how to make html click maps with Matplotlib agg PNGs. We would
# also like to add this functionality to SVG. If you are interested in
# contributing to these efforts that would be great.
0