Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import flask and required functions
- from flask import Flask,render_template, redirect, url_for, request,session
- #import pandas
- import pandas as pd
- #import bokeh components
- from bokeh.embed import server_document,components
- from bokeh.layouts import column,widgetbox
- from bokeh.models import ColumnDataSource,HoverTool,GMapPlot, GMapOptions, ColumnDataSource, Circle, DataRange1d, Select
- from bokeh.server.server import Server
- from tornado.ioloop import IOLoop
- from bokeh.io import curdoc
- from bokeh.plotting import *
- from bokeh.command.util import build_single_handler_applications
- #torado components
- import tornado.wsgi
- import tornado.httpserver
- import tornado.ioloop
- import tornado.options
- import tornado.autoreload
- import sys
- from collections import OrderedDict
- import threading
- import time
- #define flask application and enable debugging
- app = Flask(__name__)
- app.debug = True
- #variables for file identification search and connection
- files = ['bokeh\\timeseries.py','bokeh\\map.py']
- argvs = {}
- urls = []
- for i in files:
- argvs[i]=None
- urls.append(i.split('\\')[-1].split('.')[0])
- host = 'localhost'
- app_port = 5567
- bokeh_port = 6060
- #function which runs bokeh server
- def run_bokeh_server(bok_io_loop):
- ##turn file paths into bokeh apps
- apps = build_single_handler_applications(files,argvs)
- ##args lifted from bokeh serve call to Server, with the addition of my own io_loop
- kwags = {
- 'io_loop':bok_io_loop,
- 'generade_session_ids':True,
- 'redirect_root':True,
- 'use_x_headers':False,
- 'secret_key':None,
- 'num_procs':1,
- 'host':['%s:%d'%(host,app_port),'%s:%d'%(host,bokeh_port)],
- 'sign_sessions':False,
- 'develop':False,
- 'port':bokeh_port,
- 'use_index':True
- }
- srv = Server(apps,**kwags)
- #add navigation
- @app.route("/")
- #add another route for easier access to index
- @app.route("/home")
- def index():
- return render_template('index.html')
- @app.route("/predict")
- def predictions():
- return "<h1>Predictions Based on PSNI Collected Data</h1>"
- @app.route("/analysis")
- def analysis():
- #get features
- return render_template('data.html')
- @app.route("/analysis/timeSeries",methods=['GET'])
- def getTimePlot():
- #pull the bokeh server app
- bokeh_scripts = {}
- for plot in urls:
- bokeh_scripts[plot]=server_document("http://localhost:5100/timeseries") # pulls the bokeh apps off of the bokeh server
- #order the plot
- all_divs= OrderedDict()
- all_divs.update(bokeh_scripts)
- all_divs = OrderedDict(sorted(all_divs.items(), key=lambda x: x[0]))
- #display the plot on jinja2 template
- return render_template('displaytimeseries.html',div_dict=all_divs)
- @app.route('/crimeMap', methods=['GET'])
- def getCrimeMap():
- #pull the bokeh server app
- bokeh_scripts = {}
- for plot in urls:
- bokeh_scripts[plot]=server_document("http://localhost:5200/map") # pulls the bokeh apps off of the bokeh server
- #order the plot
- all_divs= OrderedDict()
- all_divs.update(bokeh_scripts)
- all_divs = OrderedDict(sorted(all_divs.items(), key=lambda x: x[0]))
- #display the plot on jinja2 template
- return render_template('displaymap.html',div_dict=all_divs)
- #print errors on timeout or incorrect setup
- def rest_of_tornado(io_loop_here):
- print('starting countdown')
- time.sleep(300)
- print('countdown finished')
- io_loop.stop()
- if __name__ == "__main__":
- #initialize the tornado server
- http_server = tornado.httpserver.HTTPServer(
- tornado.wsgi.WSGIContainer(app)
- )
- http_server.listen(app_port)
- io_loop = tornado.ioloop.IOLoop.instance()
- tornado.autoreload.start(io_loop)
- #call the turn off test
- nadostop = threading.Thread(target=rest_of_tornado,args=(io_loop,))
- nadostop.start()
- #add the io_loop to the bokeh server
- run_bokeh_server(io_loop)
- print('starting the server on http://%s:%d/'%(host,app_port))
- #run the bokeh server
- io_loop.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement