Write SVG graphs with matplotlib on heroku

Introduction

I wrote something similar before, but again.

Preparing the pyenv environment

Install pyenv and pyenv-virtualenv with homebrew.

$ mkdir qiita-sin
$ cd qiita-sin
$ pyenv virtualenv 2.7.8 qiita-sin
$ pyenv local qiita-sin

Install the required libraries and get ready to run on Heroku

$ pip install flask
$ pip install gunicorn
$ pip install matplotlib
$ echo python-2.7.8 > runtime.txt
$ pip freeze > requirements.txt
$ echo web: gunicorn app:app --log-file=- > Procfile
$ mkdir templates

Template, app preparation

template

sin.html


<!DOCTYPE html>
<html lang="ja">
    <head>
        <title>sin curve test</title>
    </head>
    <body>
        <h1>sin curve test</h1>
        {% autoescape false %}{{svgstr}}{% endautoescape %}
    </body>
</html>

App

app.py


#!/bin/env python
# coding: utf-8

import os
import StringIO

from flask import Flask, render_template
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np

app = Flask(__name__)
app.debug = True

@app.route('/')
def do_sin():
    x = np.arange(-np.pi, np.pi, 0.1)
    y = np.sin(x)

    fig = plt.figure()
    plt.plot(x, y, label="sin")
    plt.legend(loc="best")

    strio = StringIO.StringIO()
    fig.savefig(strio, format="svg")
    plt.close(fig)

    strio.seek(0)
    svgstr = strio.buf[strio.buf.find("<svg"):]

    return render_template("sin.html", svgstr=svgstr.decode("utf-8"))

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 5000))
    app.run(port=port)

Deploy

Add to git

$ git init
$ echo .python-version > .gitignore
$ git add .
$ git commit -m "initial commit" .

Deploy to heroku

$ heroku create
$ git push heroku master

Coffee break for a while because there is compilation of numpy and matplotlib

Complete

Verification

$ heroku open

https://limitless-garden-3527.herokuapp.com/ opens

point

comment

For some time, the Cedar-14 stack and https were the defaults.

Recommended Posts

Write SVG graphs with matplotlib on heroku
Create SVG graph with matplotlib on heroku (displayed in Japanese)
Animate multiple graphs with matplotlib
Write charts in real time with Matplotlib on Jupyter notebook
Draw Japanese with matplotlib on Ubuntu
How to run matplotlib on heroku
Easy to draw graphs with matplotlib
Write a stacked histogram with matplotlib
Display Japanese graphs with VS Code + matplotlib
Launch Flask application with Docker on Heroku
Write a nice pie chart with matplotlib
Redis on Heroku
Drawing tips with matplotlib on the server side
[Python] How to draw multiple graphs with Matplotlib
shimehari on heroku
Animation with matplotlib
Create interactive 3D graphs on JupyterLab using matplotlib
Japanese with matplotlib
Animation with matplotlib
Histogram with matplotlib
Until you use PhantomJS with Python on Heroku
Animate with matplotlib
Visualize grib2 on a map with python (matplotlib)
[Python] Customize Colormap when drawing graphs with matplotlib
Scraping with Python, posting on TwitterBot, regular execution on Heroku
(For those unfamiliar with Matplotlib) Tips for drawing graphs with Seaborn
2-axis plot with Matplotlib
Labeled graphs on NetworkX
Heatmap with Python + matplotlib
Band graph with matplotlib
Learn with Cheminformatics Matplotlib
Real-time drawing with matplotlib
Various colorbars with Matplotlib
3D plot with matplotlib
Overlay graphs with sympy
Adjust axes with matplotlib
Multiple file processing with Kivy + Matplotlib + Draw Graph on GUI
Dynamically generate graphs with matplotlib and embed in PDF with reporlab
A story that I had a hard time displaying graphs on top of each other with matplotlib