Skip to content

Instantly share code, notes, and snippets.

@saliksyed
Last active August 14, 2017 08:42
Show Gist options
  • Select an option

  • Save saliksyed/7b78248f6e4dea9a67b48f78d5fa651e to your computer and use it in GitHub Desktop.

Select an option

Save saliksyed/7b78248f6e4dea9a67b48f78d5fa651e to your computer and use it in GitHub Desktop.
from flask import Flask, make_response
app = Flask(__name__)
import matplotlib.pyplot as plt
def draw_picture():
# Open the file with all the airports and read every line
data = open("airports.dat", "r").readlines()
# make an array to store the final data
final_data = []
# go through each row and extract the airport altitude
for row in data:
row_items = row.split(",") # split by the comma
final_data.append(float(row_items[8]))
num_bins = 50
fig, ax = plt.subplots()
# the histogram of the data
n, bins, patches = ax.hist(final_data, num_bins, normed=0)
# add labels
ax.set_xlabel('Altitude')
ax.set_ylabel('Number of Airports')
ax.set_title(r'Histogram of Elevations of Airports')
# Tweak spacing to prevent clipping of ylabel
fig.tight_layout()
return fig
@app.route("/simple.png")
def simple():
import StringIO
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
canvas=FigureCanvas(draw_picture())
png_output = StringIO.StringIO()
canvas.print_png(png_output)
response=make_response(png_output.getvalue())
response.headers['Content-Type'] = 'image/png'
return response
if __name__ == "__main__":
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment