-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_app.py
189 lines (152 loc) · 6.58 KB
/
data_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#region imports
# BASE PYTHON
from datetime import datetime
from enum import Enum
import logging
import os
# THIRD PARTY
from flask import Flask, jsonify, redirect, render_template, request, session, url_for
from flask_wtf import FlaskForm
import pandas
from tornado.ioloop import IOLoop
from wtforms import FileField, SelectMultipleField, StringField, SubmitField
from bokeh.application import Application
from bokeh.application.handlers import FunctionHandler
from bokeh.embed import server_document
from bokeh.server.server import Server
# USER DEFINED
import CommonLogging
import param_plotting
import param_stats
import session_info
#endregion
"""
Data comparison web application for comparing common parameters across two independent
csv files.
Remote Repo: https://github.com/jxramos/DataApp--ParamCompare
"""
#region GLOBALS
launch_time = datetime.now()
app_name = os.path.basename( __file__ ).replace( '.py' , '' )
logger = logging.getLogger(__name__)
class ParamTypes(Enum) :
p1 = "Param1"
p2 = "Param2"
p3 = "Param3"
# Flask infrastructure
flask_app = Flask(__name__)
flask_app.config['SECRET_KEY'] = 'the secret to life'
flask_app.jinja_env.trim_blocks = True
flask_app.jinja_env.lstrip_blocks = True
class CompareInputForm(FlaskForm):
id_col = StringField("ID", default="id")
file_x = FileField( "File X" )
file_y = FileField( "File Y" )
label_x = StringField('Label X')
label_y = StringField('Label Y')
params = SelectMultipleField( "Param Selection", choices=[ (e.name, e.value) for e in ParamTypes ] )
stats = SelectMultipleField( "Statistic Selection", choices=[ (e.name, e.value) for e in param_stats.StatTypes ] )
submit = SubmitField('Submit')
# Bokeh infrastructure
bokeh_lin_reg_app = Application(FunctionHandler(param_plotting.lin_reg_plot))
bokeh_data_explore_app = Application(FunctionHandler(param_plotting.data_explore_plot))
io_loop = IOLoop.current()
stat_type_2_plot_route = { param_stats.StatTypes.lin_reg.name : 'bk_lin_reg_app' }
data_explore_route = 'data_explore'
server = Server( { f'/{stat_type_2_plot_route[param_stats.StatTypes.lin_reg.name]}': bokeh_lin_reg_app , # Linear Regression App
f'/{data_explore_route}' : bokeh_data_explore_app ,
},
io_loop=io_loop, allow_websocket_origin=["localhost:8080"])
server.start()
#endregion
@flask_app.route('/', methods=['GET', "POST"] )
def index():
"""
Main page where user submits all required input to conduct a csv to csv
comparison analysis of common columns.
"""
logger.debug("")
session_info.init_session( session )
form = CompareInputForm()
if request.method == "POST" :
if form.errors :
logger.debug(form.errors)
# Populate model with form data
model = session_info.get_user_model(session)
model.load_model( form )
# Execute parameter comparison
model.compare_parameters()
return redirect( url_for('summary_page' ) )
return render_template( 'index.html' , form=form )
@flask_app.route( '/summary', methods=["GET"])
def summary_page() :
"""
The top level summary page where all found parameter result comparisons are presented
to the user.
"""
logger.debug("")
model = session_info.get_user_model(session)
return render_template( "summary_page.html" , model=model ,
stat_types=param_stats.StatTypes )
@flask_app.route( '/data/<table_type>', methods=["GET"])
def data_table_page( table_type ) :
"""
Renders table presentation of data
"""
logger.debug( f"table_type={table_type}" )
model = session_info.get_user_model(session)
# select table type's corresponding data
if table_type == "x" :
df = model._dfX
elif table_type== "y" :
df = model._dfY
elif table_type == "merged" :
df = model.dfMerged
elif table_type == "param" :
param = request.args["param"]
logger.debug(f"param={param}")
df = model.dfMerged[[ model.id_col , f"{param}_x", f"{param}_y"]]
else :
logger.debug()
raise ValueError( f"Unrecognized table_type={table_type}" )
return f"<pre>{df.to_string()}</pre>" # TODO replace with template
@flask_app.route( '/plot/<stat_type>', methods=["GET"])
def plot_page( stat_type ) :
"""
Renders table presentation of data
"""
logger.debug( f"stat_type={stat_type}" )
param = request.args["param"]
script = server_document( url=f'http://localhost:5006/{stat_type_2_plot_route[stat_type]}',
arguments={'param' : param ,
'stat_type' : stat_type ,
'session_id' : session[ session_info.session_id_key ] } )
return render_template('plot_page.html',
script=script ,
param=param ,
stat_type=param_stats.StatTypes[stat_type].value )
@flask_app.route( '/sample/<sample_id>' , methods=['GET'] )
def sample_page( sample_id ) :
return f"<h1>Sample Data Point</h1><br><pre>Sample: {sample_id}</pre>" # TODO simulate some sample expansion behavior.
@flask_app.route('/data-pair' , methods=['GET'])
def data_pair_page() :
"""
Renders the interactive scatter plot page where arbitrary pairs of numerical
columns of the merged DataFrame can be plotted side by side.
"""
script = server_document( url=f'http://localhost:5006/{data_explore_route}',
arguments={'session_id' : session[ session_info.session_id_key ] } )
return render_template( 'data_explore_page.html', script=script )
if __name__ == '__main__':
from tornado.httpserver import HTTPServer
from tornado.wsgi import WSGIContainer
from bokeh.util.browser import view
launch_time_str = datetime.strftime( launch_time , '%Y-%m-%d_%H.%M.%S' )
CommonLogging.setup_logger( os.getcwd() , launch_time_str , app_name )
logger.info('Opening Flask app with embedded Bokeh application on http://localhost:8080/')
# This uses Tornado to server the WSGI app that flask provides. Presumably the IOLoop
# could also be started in a thread, and Flask could server its own app directly
http_server = HTTPServer(WSGIContainer(flask_app))
http_server.listen(8080)
io_loop.add_callback(view, "http://localhost:8080/")
io_loop.start()