-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
135 lines (115 loc) · 4.87 KB
/
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
__author__ = "konwar.m"
__copyright__ = "Copyright 2022, AI R&D"
__credits__ = ["konwar.m"]
__license__ = "Individual Ownership"
__version__ = "1.0.1"
__maintainer__ = "konwar.m"
__email__ = "[email protected]"
__status__ = "Development"
import os
import sys
import dash
import sqlite3
import logging, logging.config
import dash_bootstrap_components as dbc
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from config.applogger import app_loggers
from sqlalchemy import Table, create_engine
from callbacks.callbacks_authentication import callback_manager as authentication_callback_manager
from callbacks.callbacks_sidepanel import callback_manager as sidepanel_callback_manager
from callbacks.callbacks_retail_summary import callback_manager as retail_summary_callback_manager
from callbacks.callbacks_pricing_input import callback_manager as pricing_input_callback_manager
from callbacks.callbacks_pricing_sales import callback_manager as prediction_output_callback_manager
from callbacks.callbacks_pivot_kpis import callback_manager as pivotted_kpi_callback_manager
from callbacks.callbacks_kpis import callback_manager as kpi_callback_manager
from utility.utility_authentication import User, create_users_table
from utility.utility_tasks import Task, create_tasks_table
# SQL Alchemy DB instance to use it under models
db = SQLAlchemy()
# Create Db if it does not exists
conn = sqlite3.connect('data.sqlite')
engine = create_engine('sqlite:///data.sqlite')
# Intitate the user and task table instances
users_tbl = Table('users', User.metadata)
tasks_tbl = Table('tasks', Task.metadata)
# Static uploading folder
uploaded_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tasks', 'upload')
if not os.path.exists(uploaded_path):
os.makedirs(uploaded_path)
# Normally Dash creates its own Flask server internally however
# by creating the server we can easily create routes for downloading files etc.
def create_app():
dbc_css = ("https://cdn.jsdelivr.net/gh/AnnMarieW/[email protected]/dbc.min.css")
external_stylesheets = [dbc.themes.BOOTSTRAP, dbc_css]
server = Flask(__name__)
app = dash.Dash(external_stylesheets=external_stylesheets, server=server)
app.config.suppress_callback_exceptions = True
# DB configuration
app.server.config.update(
SECRET_KEY=os.urandom(12),
SQLALCHEMY_DATABASE_URI='sqlite:///data.sqlite',
SQLALCHEMY_TRACK_MODIFICATIONS=False
)
# Instantiate db
db.init_app(app.server)
# Instantiate Login Manager
login_manager = LoginManager()
login_manager.login_view = '/login'
login_manager.init_app(app.server)
# callback to reload the user object
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
# create the user table in db
create_users_table(engine)
# create the task table in db
create_tasks_table(engine)
return app
app = create_app()
# Attaching tab based callbacks to app
authentication_callback_manager.attach_to_app(app)
sidepanel_callback_manager.attach_to_app(app)
retail_summary_callback_manager.attach_to_app(app)
pricing_input_callback_manager.attach_to_app(app)
prediction_output_callback_manager.attach_to_app(app)
pivotted_kpi_callback_manager.attach_to_app(app)
kpi_callback_manager.attach_to_app(app)
# Adding log folder if it not exists
if not os.path.exists('logs'):
os.makedirs('logs')
# Adding log handlers for simulator
logging.config.dictConfig(app_loggers)
'''
logger = logging.getLogger('pricing_handler')
# Segregating Dash Application Environment based on Configuration
if len(sys.argv)>1:
if sys.argv[1] == "testing":
print('Setting app config as Testing')
app.server.config["ENV"] = "testing"
elif sys.argv[1] == "development":
print('Setting app config as Development')
app.server.config["ENV"] = "development"
elif sys.argv[1] == 'deployment':
print('Setting app config as Deployment')
app.server.config["ENV"] = "deployment"
logger.info("Setting app config with %s Instance" %(sys.argv[1]))
'''
# Configuring Flask Server based on ENV type
if app.server.config["ENV"] == "production":
print('Setting Production Configurations')
app.server.config.from_object("config.appconfig.ProductionConfig")
os.environ["ENV"] = "production"
elif app.server.config["ENV"] == "testing":
print('Setting Testing Configurations')
app.server.config.from_object("config.appconfig.TestingConfig")
os.environ["ENV"] = "testing"
elif app.server.config["ENV"] == "development":
print('Setting Development Configurations')
app.server.config.from_object("config.appconfig.DevelopmentConfig")
os.environ["ENV"] = "development"
else:
print('Setting Deployment Configurations')
app.server.config.from_object("config.appconfig.DeploymentConfig")
os.environ["ENV"] = "deployment"