I wrote something similar before, but again.
Install pyenv and pyenv-virtualenv with homebrew.
$ mkdir qiita-sin
$ cd qiita-sin
$ pyenv virtualenv 2.7.8 qiita-sin
$ pyenv local qiita-sin
$ 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
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.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)
$ git init
$ echo .python-version > .gitignore
$ git add .
$ git commit -m "initial commit" .
$ heroku create
$ git push heroku master
Coffee break for a while because there is compilation of numpy and matplotlib
$ heroku open
https://limitless-garden-3527.herokuapp.com/ opens
(strio.buf.find ("<svg")
and decode ("utf-8 ")
are the pointsFor some time, the Cedar-14 stack and https were the defaults.
Recommended Posts