forked from thedataincubator/flask-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Render plot on page, interactivity not working yet.
- Loading branch information
Showing
2 changed files
with
46 additions
and
18 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,59 @@ | ||
"""Display a stock ticker visualization based on user input.""" | ||
import json | ||
from flask import Flask, render_template, request, redirect | ||
from bokeh.embed import json_item | ||
from bokeh.resources import CDN | ||
from ticker import ticker | ||
|
||
|
||
app = Flask(__name__) | ||
app.vars = {} | ||
|
||
|
||
def create_ticker(symbol): | ||
"""Render ticker json object from stock symbol.""" | ||
p = ticker(symbol) | ||
|
||
return p | ||
|
||
|
||
@app.route('/', methods=('GET', 'POST')) | ||
def index(): | ||
"""Render the app's main page.""" | ||
if request.method == 'GET': | ||
# Default gives IBM's ticker when first loaded. | ||
symbol = 'IBM' | ||
return render_template('index.html', symbol=symbol) | ||
app.vars['symbol'] = 'IBM' | ||
p = create_ticker(app.vars['symbol']) | ||
p_json = json.dumps(json_item(p, 'myplot')) | ||
return render_template('index.html', | ||
symbol=app.vars['symbol'], | ||
p=p, | ||
p_json=p_json, | ||
) | ||
|
||
else: | ||
# User submitted a ticker symbol aka method = 'POST' | ||
symbol = request.form['symbol'] | ||
return render_template('index.html', symbol=symbol) | ||
app.vars['symbol'] = request.form['symbol'] | ||
p = create_ticker(app.vars['symbol']) | ||
json.dumps(json_item(p, 'myplot')) | ||
return render_template('index.html', | ||
symbol=app.vars['symbol'], | ||
p=p, | ||
p_json=p_json, | ||
) | ||
|
||
|
||
@app.route('/about') | ||
def about(): | ||
return render_template('about.html') | ||
|
||
|
||
@app.route('/plot') | ||
def plot(): | ||
"""Render json item from bokeh plot.""" | ||
p = create_ticker(app.vars['symbol']) | ||
return json.dumps(json_item(p, 'myplot')) | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run(port=33507) |
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