-
-
Save lorinma/2530ee41e802c168b282 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python2 | |
'''Serving dynamic images with Pandas and matplotlib (using flask).''' | |
import matplotlib | |
matplotlib.use('Agg') | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import pandas as pd | |
from cStringIO import StringIO | |
import base64 | |
from flask import Flask | |
app = Flask(__name__) | |
html = ''' | |
<html> | |
<body> | |
<img src="data:image/png;base64,{}" /> | |
</body> | |
</html> | |
''' | |
@app.route("/") | |
def hello(): | |
df = pd.DataFrame( | |
{'y':np.random.randn(10), 'z':np.random.randn(10)}, | |
index=pd.period_range('1-2000',periods=10), | |
) | |
fig = plt.figure() | |
ax = fig.add_subplot(1, 1, 1) | |
df.plot(ax=ax) | |
io = StringIO() | |
fig.savefig(io, format='png') | |
data = base64.encodestring(io.getvalue()) | |
return html.format(data) | |
if __name__ == '__main__': | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment