8000 Turn "How to use Matplotlib in a web application server" into a sphin… · matplotlib/matplotlib@52aa998 · GitHub
[go: up one dir, main page]

Skip to content

Commit 52aa998

Browse files
committed
Turn "How to use Matplotlib in a web application server" into a sphinx-gallery example
1 parent cf83ec4 commit 52aa998

File tree

2 files changed

+55
-52
lines changed

2 files changed

+55
-52
lines changed

doc/faq/howto_faq.rst

Lines changed: 0 additions & 52 deletions
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.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
@app.route("/")
29+
def hello():
30+
# Generate the figure **without using pyplot**.
31+
fig = Figure()
32+
ax = fig.subplots()
33+
ax.plot([1, 2])
34+
# Save it to a temporary buffer.
35+
buf = BytesIO()
36+
fig.savefig(buf, format="png")
37+
# Embed the result in the html output.
38+
data = base64.b64encode(buf.getbuffer()).decode("ascii")
39+
return f"<img src='data:image/png;base64,{data}'/>"
40+
41+
# %%
42+
# When using Matplotlib versions older than 3.1, it is necessary to explicitly
43+
# instantiate an Agg canvas; see e.g. :doc:`/gallery/user_interfaces/canvasagg`.
44+
#
45+
# .. _howto-click-maps:
46+
#
47+
# Clickable images for HTML
48+
# -------------------------
49+
#
50+
# Andrew Dalke of `Dalke Scientific <http://www.dalkescientific.com>`_
51+
# has written a nice `article
52+
# <http://www.dalkescientific.com/writings/diary/archive/2005/04/24/interactive_html.html>`_
53+
# on how to make html click maps with Matplotlib agg PNGs. We would
54+
# also like to add this functionality to SVG. If you are interested in
55+
# contributing to these efforts that would be great.

0 commit comments

Comments
 (0)
0