Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.82 KB | None | 0 0
  1. #import flask and required functions
  2. from flask import Flask,render_template, redirect, url_for, request,session
  3. #import pandas
  4. import pandas as pd
  5. #import bokeh components
  6. from bokeh.embed import server_document,components
  7. from bokeh.layouts import column,widgetbox
  8. from bokeh.models import ColumnDataSource,HoverTool,GMapPlot, GMapOptions, ColumnDataSource, Circle, DataRange1d, Select
  9. from bokeh.server.server import Server
  10. from tornado.ioloop import IOLoop
  11. from bokeh.io import curdoc
  12. from bokeh.plotting import *
  13. from bokeh.command.util import build_single_handler_applications
  14.  
  15. #torado components
  16. import tornado.wsgi
  17. import tornado.httpserver
  18. import tornado.ioloop
  19. import tornado.options
  20. import tornado.autoreload
  21. import sys
  22. from collections import OrderedDict
  23. import threading
  24. import time
  25.  
  26. #define flask application and enable debugging
  27. app = Flask(__name__)
  28. app.debug = True
  29. #variables for file identification search and connection
  30. files = ['bokeh\\timeseries.py','bokeh\\map.py']
  31. argvs = {}
  32. urls = []
  33. for i in files:
  34.     argvs[i]=None
  35.     urls.append(i.split('\\')[-1].split('.')[0])
  36. host = 'localhost'
  37. app_port = 5567
  38. bokeh_port = 6060
  39. #function which runs bokeh server
  40. def run_bokeh_server(bok_io_loop):
  41.     ##turn file paths into bokeh apps
  42.     apps = build_single_handler_applications(files,argvs)
  43.     ##args lifted from bokeh serve call to Server, with the addition of my own io_loop
  44.     kwags = {
  45.         'io_loop':bok_io_loop,
  46.         'generade_session_ids':True,
  47.         'redirect_root':True,
  48.         'use_x_headers':False,
  49.         'secret_key':None,
  50.         'num_procs':1,
  51.         'host':['%s:%d'%(host,app_port),'%s:%d'%(host,bokeh_port)],
  52.         'sign_sessions':False,
  53.         'develop':False,
  54.         'port':bokeh_port,
  55.         'use_index':True
  56.     }
  57.     srv = Server(apps,**kwags)
  58.  
  59. #add navigation
  60. @app.route("/")
  61. #add another route for easier access to index
  62. @app.route("/home")
  63. def index():
  64.     return render_template('index.html')
  65.  
  66. @app.route("/predict")
  67. def predictions():
  68.     return "<h1>Predictions Based on PSNI Collected Data</h1>"
  69.  
  70. @app.route("/analysis")
  71. def analysis():
  72.     #get features
  73.     return render_template('data.html')
  74.  
  75. @app.route("/analysis/timeSeries",methods=['GET'])
  76. def getTimePlot():
  77.     #pull the bokeh server app
  78.     bokeh_scripts = {}
  79.     for plot in urls:
  80.         bokeh_scripts[plot]=server_document("http://localhost:5100/timeseries") # pulls the bokeh apps off of the bokeh server
  81.  
  82.     #order the plot
  83.     all_divs= OrderedDict()
  84.     all_divs.update(bokeh_scripts)
  85.     all_divs = OrderedDict(sorted(all_divs.items(), key=lambda x: x[0]))
  86.  
  87.     #display the plot on jinja2 template
  88.     return render_template('displaytimeseries.html',div_dict=all_divs)
  89.    
  90. @app.route('/crimeMap', methods=['GET'])
  91. def getCrimeMap():
  92.    #pull the bokeh server app
  93.     bokeh_scripts = {}
  94.     for plot in urls:
  95.         bokeh_scripts[plot]=server_document("http://localhost:5200/map") # pulls the bokeh apps off of the bokeh server
  96.  
  97.     #order the plot
  98.     all_divs= OrderedDict()
  99.     all_divs.update(bokeh_scripts)
  100.     all_divs = OrderedDict(sorted(all_divs.items(), key=lambda x: x[0]))
  101.  
  102.     #display the plot on jinja2 template
  103.     return render_template('displaymap.html',div_dict=all_divs)
  104.    
  105. #print errors on timeout or incorrect setup
  106. def rest_of_tornado(io_loop_here):
  107.     print('starting countdown')
  108.     time.sleep(300)
  109.     print('countdown finished')
  110.     io_loop.stop()
  111.  
  112. if __name__ == "__main__":
  113.     #initialize the tornado server
  114.     http_server = tornado.httpserver.HTTPServer(
  115.         tornado.wsgi.WSGIContainer(app)
  116.     )
  117.     http_server.listen(app_port)
  118.     io_loop = tornado.ioloop.IOLoop.instance()
  119.     tornado.autoreload.start(io_loop)
  120.  
  121.     #call the turn off test
  122.     nadostop = threading.Thread(target=rest_of_tornado,args=(io_loop,))
  123.     nadostop.start()
  124.    
  125.     #add the io_loop to the bokeh server
  126.     run_bokeh_server(io_loop)
  127.     print('starting the server on http://%s:%d/'%(host,app_port))
  128.  
  129.     #run the bokeh server
  130.     io_loop.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement