Last active
January 30, 2018 15:56
-
-
Save se1983/f2602865a9d6b673e747dd7852b81c6b to your computer and use it in GitHub Desktop.
Using flask to deliver plt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from io import BytesIO | |
from flask import Flask, make_response, render_template | |
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas | |
from BA_argo_playground.plot_image import create_plot | |
app = Flask(__name__, template_folder="/home/sebsch/Dokumente/Uni-Workdir/Bachelorarbeit/BA_argo_playground/templates") | |
@app.route("/simple.png") | |
def simple(): | |
fig = create_plot() | |
canvas=FigureCanvas(fig) | |
png_output = BytesIO() | |
canvas.print_png(png_output) | |
response=make_response(png_output.getvalue()) | |
response.headers['Content-Type'] = 'image/png' | |
return response | |
@app.route('/') | |
def images(): | |
return render_template("images.html", title="Test") | |
if __name__ == "__main__": | |
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>{{ title }} - image</title> | |
</head> | |
<body> | |
<img src="{{ url_for('simple') }}" alt="Image Placeholder" width="100%"> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from matplotlib.figure import Figure | |
from matplotlib.dates import DateFormatter | |
import datetime | |
import random | |
def create_plot(): | |
fig = Figure() | |
ax = fig.add_subplot(111) | |
x = [] | |
y = [] | |
now = datetime.datetime.now() | |
delta = datetime.timedelta(days=1) | |
for i in range(10): | |
x.append(now) | |
now += delta | |
y.append(random.randint(0, 1000)) | |
ax.plot_date(x, y, '-') | |
ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d')) | |
fig.autofmt_xdate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment