Simple Flask App

Tue 15 August 2017 | tags: python, web extraction, flask, heroku, bokeh, quandl, requests, pandas, -- (permalink)

Heres a simple Flask App I built that uses quandl's API to return the recent history of a stock. The finished app can be found here.

The github for my project's code is also found here.

The Objective

I just wanted to get an idea of how Flask and python works for deploying python apps on Heroku.

The Information Source

Quandl provides a nice free API (although sometimes buggy) where you can view the history of a stock. Information such as closing price and dates are available.

The Process

The idea was to use Flask to build a simple app.

Flask apps have a general similar structure. Heres a quick rundown.

Generate the necessary folder/file structure which generally looks like this:

/flaskapp
    /templates
    -app.py
    -Procfile
    -requirements.txt
    -runtime.txt
  • app.py This is where the main python code lives, these flask apps also have a general structure to their python code too.

It looks similar to what is shown below. This simple app would return a html page that lives in the/templates folder.

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')

def home():
    return render_template("home.html")
if __name__ == "main"
    app.run(debug = True)

The rest of these files are really related to Heroku and your deployment process.

  • Procfile In my app I used gunicorn to point to where the app is for Heroku.
web: gunicorn app:app
  • runtime.txt This text file points out what verison of Python Heroku should deploy for your app.

  • requirements.txt Its in this text file that you point out what python packages should be installed.

For this stock history project, I used Flask's ability to handle GET and POST methods from user inputs from on the frontend html. These inputs were stock ticker indexes like "GOOGL", "F", "APPL".

Information about these stocks were retrieved using the QUANDL api and stored in a pandas data frame which was then plotted using bokeh. Finally output was displayed on the frontend using bokehJS and a Content Delivery Network (CDN).

All this can be seen clearly in my github code.

Thanks for reading. :)