Answer the question
In order to leave comments, you need to log in
How to plot in Flask on Google App Engine?
Hello.
To build a graph, I use this recipe (first building and saving in .png, then .png in the HTML body is taken from the folder).
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
plt.plot(x_1, y_1, 'r-',
x_2, y_2, 'b-')
plt.savefig('static/img/plot.png')
<img src="static/img/plot.png" alt="Image Placeholder" height="400">
Answer the question
In order to leave comments, you need to log in
I'll answer my own question.
Perhaps for the same lamers like me, it will be useful.
1. Before using any library in GAE, you need to make sure if it is supported or not. Follow this link: https://cloud.google.com/appengine/docs/python/too...
2. If it is supported (as in the case of matplotlib), look to what extent: look Supported version. We have this "1.2.0" "latest"
3. Based on this, we adjust our app.yaml libraries file :
-
name: jinja2
version: "2.6"
- name: markupsafe
version: "0.15"
- name: matplotlib
version: "latest "
4. Then already in our main.py (well, or views.py - depending on how you called the file that processes the views and depending on the project structure) we do the import as usual
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import StringIO
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
def func():
plt.plot(x1, y1, 'r-',
x2, y2 'b-')
output = StringIO.StringIO()
plt.savefig(output, format="png")
img = 'data:image/png;base64,%s' % output.getvalue().encode("base64").strip()
return img
## Само представление
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
@app.route('/index')
def index():
img = func() ## Я создал отдельную функцию (см. выше) для формирования и вызываю её при заходе на страницу
return render_template(
'index.html',
img = img)
<img src="{{ img }}" alt="Image Placeholder" height="400">
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question