Skip to content

Commit d872350

Browse files
authored
Merge pull request #18767 from Transfusion/fix_18301
Turn "How to use Matplotlib in a web application server" into a sphinx-gallery example
2 parents d7fc51b + 30330fc commit d872350

File tree

2 files changed

+75
-52
lines changed

2 files changed

+75
-52
lines changed

doc/faq/howto_faq.rst

-52
Original file line numberDiff line numberDiff line change
@@ -526,55 +526,3 @@ artists.
526526
You may be able to work on separate figures from separate threads. However,
527527
you must in that case use a *non-interactive backend* (typically Agg), because
528528
most GUI backends *require* being run from the main thread as well.
529-
530-
.. _howto-webapp:
531-
532-
How to use Matplotlib in a web application server
533-
=================================================
534-
535-
In general, the simplest solution when using Matplotlib in a web server is
536-
to completely avoid using pyplot (pyplot maintains references to the opened
537-
figures to make `~.matplotlib.pyplot.show` work, but this will cause memory
538-
leaks unless the figures are properly closed). Since Matplotlib 3.1, one
539-
can directly create figures using the `.Figure` constructor and save them to
540-
in-memory buffers. The following example uses Flask_, but other frameworks
541-
work similarly::
542-
543-
import base64
544-
from io import BytesIO
545-
546-
from flask import Flask
547-
from matplotlib.figure import Figure
548-
549-
app = Flask(__name__)
550-
551-
@app.route("/")
552-
def hello():
553-
# Generate the figure **without using pyplot**.
554-
fig = Figure()
555-
ax = fig.subplots()
556-
ax.plot([1, 2])
557-
# Save it to a temporary buffer.
558-
buf = BytesIO()
559-
fig.savefig(buf, format="png")
560-
# Embed the result in the html output.
561-
data = base64.b64encode(buf.getbuffer()).decode("ascii")
562-
return f"<img src='data:image/png;base64,{data}'/>"
563-
564-
.. _Flask: http://flask.pocoo.org/
565-
566-
When using Matplotlib versions older than 3.1, it is necessary to explicitly
567-
instantiate an Agg canvas; see e.g. :doc:`/gallery/user_interfaces/canvasagg`.
568-
569-
570-
.. _howto-click-maps:
571-
572-
Clickable images for HTML
573-
-------------------------
574-
575-
Andrew Dalke of `Dalke Scientific <http://www.dalkescientific.com>`_
576-
has written a nice `article
577-
<http://www.dalkescientific.com/writings/diary/archive/2005/04/24/interactive_html.html>`_
578-
on how to make html click maps with Matplotlib agg PNGs. We would
579-
also like to add this functionality to SVG. If you are interested in
580-
contributing to these efforts that would be great.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""
2+
.. _howto-webapp:
3+
4+
=================================================
5+
How to use Matplotlib in a web application server
6+
=================================================
7+
8+
In general, the simplest solution when using Matplotlib in a web server is
9+
to completely avoid using pyplot (pyplot maintains references to the opened
10+
figures to make `~.matplotlib.pyplot.show` work, but this will cause memory
11+
leaks unless the figures are properly closed). Since Matplotlib 3.1, one
12+
can directly create figures using the `.Figure` constructor and save them to
13+
in-memory buffers. The following example uses Flask_, but other frameworks
14+
work similarly:
15+
16+
.. _Flask: http://flask.pocoo.org/
17+
18+
"""
19+
20+
import base64
21+
from io import BytesIO
22+
23+
from flask import Flask
24+
from matplotlib.figure import Figure
25+
26+
app = Flask(__name__)
27+
28+
29+
@app.route("/")
30+
def hello():
31+
# Generate the figure **without using pyplot**.
32+
fig = Figure()
33+
ax = fig.subplots()
34+
ax.plot([1, 2])
35+
# Save it to a temporary buffer.
36+
buf = BytesIO()
37+
fig.savefig(buf, format="png")
38+
# Embed the result in the html output.
39+
data = base64.b64encode(buf.getbuffer()).decode("ascii")
40+
return f"<img src='data:image/png;base64,{data}'/>"
41+
42+
# %%
43+
# When using Matplotlib versions older than 3.1, it is necessary to explicitly
44+
# instantiate an Agg canvas;
45+
# see e.g. :doc:`/gallery/user_interfaces/canvasagg`.
46+
#
47+
# Note: This script should be run using the
48+
# `flask command-line tool <https://flask.palletsprojects.com/en/master/cli/>`_
49+
# since it is a Flask application.
50+
# Assuming that the working directory contains this script:
51+
#
52+
# Unix-like systems
53+
#
54+
# .. code-block:: console
55+
#
56+
# FLASK_APP=web_application_server_sgskip flask run
57+
#
58+
# Windows
59+
#
60+
# .. code-block:: console
61+
#
62+
# set FLASK_APP=web_application_server_sgskip
63+
# flask run
64+
#
65+
# .. _howto-click-maps:
66+
#
67+
# Clickable images for HTML
68+
# -------------------------
69+
#
70+
# Andrew Dalke of `Dalke Scientific <http://www.dalkescientific.com>`_
71+
# has written a nice `article
72+
# <http://www.dalkescientific.com/writings/diary/archive/2005/04/24/interactive_html.html>`_
73+
# on how to make html click maps with Matplotlib agg PNGs. We would
74+
# also like to add this functionality to SVG. If you are interested in
75+
# contributing to these efforts that would be great.

0 commit comments

Comments
 (0)