154 lines
3.9 KiB
Python
154 lines
3.9 KiB
Python
import requests
|
|
import pandas as pd
|
|
import numpy as np
|
|
import json
|
|
import os
|
|
import plotly as pl
|
|
import plotly.graph_objects as go
|
|
import plotly.io as plio
|
|
import plotly.io.orca
|
|
import plotly.express as px
|
|
from flask import Flask, request, render_template,redirect, url_for, flash
|
|
from app import app, models, utils
|
|
#import dash
|
|
|
|
# In-Memory Database
|
|
database = dict()
|
|
model_maps = {'1': 'Model1', '2': 'Model2', '3': 'Model3'}
|
|
|
|
# Global variables
|
|
offline = False # Use JavaScript: offline=False
|
|
|
|
@app.context_processor
|
|
def inject_enumerate():
|
|
return dict(enumerate=enumerate)
|
|
|
|
@app.route('/')
|
|
@app.route('/home')
|
|
def home():
|
|
return render_template('index.html')
|
|
|
|
@app.route('/scenario/<scenario>')
|
|
def load_scenario(scenario):
|
|
global database
|
|
|
|
data, columns = models.get_data(scenario)
|
|
|
|
if scenario not in database.keys():
|
|
database[scenario] = {'data':data, 'columns': columns}
|
|
|
|
return render_template(
|
|
'scenarios.html',
|
|
title = utils.get_title(scenario),
|
|
scenario = scenario,
|
|
data = data,
|
|
columns = columns,
|
|
selected = None)
|
|
|
|
@app.route('/benchmarks')
|
|
def benchmarks():
|
|
df = pd.DataFrame({
|
|
'Fruit': ['Apples', 'Oranges', 'Bananas', 'Apples', 'Oranges', 'Bananas'],
|
|
'Amount': [4, 1, 2, 2, 4, 5],
|
|
'City': ['SF', 'SF', 'SF', 'Montreal', 'Montreal', 'Montreal']
|
|
})
|
|
|
|
fig = px.bar(df, x='Fruit', y='Amount', color='City', barmode='group')
|
|
graphJSON = json.dumps(fig, cls=pl.utils.PlotlyJSONEncoder)
|
|
|
|
return render_template('benchmarks.html', graphJSON=graphJSON)
|
|
|
|
@app.route('/browse')
|
|
def browse():
|
|
return render_template('browse.html',
|
|
web_page=models.get_web_page("onion")
|
|
)
|
|
|
|
@app.route('/search')
|
|
def search():
|
|
return render_template('search.html')
|
|
|
|
@app.route('/demand')
|
|
def demand():
|
|
return render_template('demand.html')
|
|
|
|
|
|
@app.route('/graphs')
|
|
def graficos():
|
|
df,cat = models.get_data("total")
|
|
|
|
if(offline == False):
|
|
plot = online_render(df, "bar")
|
|
else:
|
|
plot = offline_render(df, "line")
|
|
|
|
return render_template('graphs.html', plot=plot)
|
|
|
|
@app.route('/reports')
|
|
def relatorio():
|
|
return render_template('reports.html')
|
|
|
|
|
|
def online_render(data, type):
|
|
if(type == "bar"):
|
|
fig = px.bar(data)
|
|
graphJSON = json.dumps(fig, cls=pl.utils.PlotlyJSONEncoder)
|
|
|
|
elif(type == "line"):
|
|
fig = px.line(data)
|
|
graphJSON = json.dumps(fig, cls=pl.utils.PlotlyJSONEncoder)
|
|
|
|
return graphJSON
|
|
|
|
def offline_render(data, type):
|
|
if(type == "bar"):
|
|
fig = px.bar()
|
|
|
|
elif(type == "line"):
|
|
fig = px.bar()
|
|
|
|
return fig
|
|
|
|
@app.route('/copy_to_form/<scenario>/<index>')
|
|
def copy_to_form(scenario, index):
|
|
global database
|
|
print('keys in database:', database.keys())
|
|
selected = database[scenario]['data'][int(index)]
|
|
|
|
return render_template(
|
|
'scenarios.html',
|
|
title = utils.get_title(scenario),
|
|
scenario = scenario,
|
|
data = database[scenario]['data'],
|
|
columns = database[scenario]['columns'],
|
|
selected = selected)
|
|
|
|
@app.route('/search', methods = ["POST"])
|
|
def search_archive():
|
|
net = request.form.get("checkbox1")
|
|
keyword = request.form.get("field")
|
|
print("checkbox1 net: ", net)
|
|
return render_template(
|
|
'search.html',
|
|
title = utils.get_title("search"),
|
|
result = models.search_archive(keyword)
|
|
)
|
|
|
|
@app.route('/enviar/exemple/<scenario>', methods = ['POST'])
|
|
def enviar_exemplo(scenario):
|
|
features = utils.get_features(request.form)
|
|
|
|
url = 'http://localhost:5001/predict?model={}&features={}'.format(model_maps[scenario], features)
|
|
|
|
print('url', url)
|
|
|
|
resultado = requests.get(url).json()[0]
|
|
return render_template(
|
|
'scenarios.html',
|
|
title = utils.get_title(scenario),
|
|
scenario = scenario,
|
|
data = database[scenario]['data'],
|
|
columns = database[scenario]['columns'],
|
|
prediction = resultado
|
|
)
|