part-1 of code (upto layout)
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import pyodbc
import dash_table
import plotly.express as px
import plotly.graph_objects as go
from dash.dependencies import Input, Output
from plotly.subplots import make_subplots
import numpy as np
import flask
from datetime import time
from flask_caching import Cache
import os
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css', 'https://codepen.io/chriddyp/pen/brPBPO.css']
server = flask.Flask(__name__)
app = dash.Dash(__name__, external_stylesheets=external_stylesheets, server=server)
cache = Cache(app.server, config={
'CACHE_TYPE': 'redis',
'CACHE_REDIS_URL': os.environ.get('REDIS_URL', '')
})
app.config.suppress_callback_exceptions = True
TIMEOUT = 30
@cache.memoize(timeout=TIMEOUT)
def query_data():
driver = "SQL Server"
server = "***.**.146.96\SQLEXPRESS"
database = "***IIOT"
username = "IIOTUser"
password = "****#13579"
# Create the connection
conn = pyodbc.connect(
"driver=" + driver + ";server=" + server + ";uid=" + username + ";pwd=" + password + ";database=" + database)
# query db
sql = """
SELECT * FROM Ichinose1
"""
df = pd.read_sql(sql, conn)
df.fillna(0, inplace=True)
df['date_time1'] = pd.to_datetime(df['shiftDate'].str[:10] + " " + df['shiftTime'].str[:8])
df.sort_values(by='date_time1', ascending=True, inplace=True)
df['e'] = np.nan
df.loc[((time(00, 00, 00) <= df['date_time1'].dt.time) & (df['date_time1'].dt.time <= time(6, 59, 59))), 'e'] = 1
df.loc[df['e'] == 1, 'e'] = df['date_time1'] - pd.DateOffset(days=1)
df.loc[pd.isnull(df['e']), 'e'] = df['shiftDate']
df['e'] = pd.to_datetime(df.loc[pd.notnull(df['e']), 'e'])
df['Date_Time'] = pd.to_datetime(df['e'].dt.date.astype(str).str[:10] + " " + df['shiftTime'].str[:8])
df['cdate'] = df['Date_Time'].dt.date.astype(str).str[:10]
df['MachineRunStatus'] = np.where(df['RunStatus'] == 1, "Running", "Idle")
df.reset_index(inplace=True, drop=True)
df['Production_Order'] = np.where(
np.array(df['RunStatus'])[np.where(df.index == df.last_valid_index(), df.index, df.index + 1)] == 0,
np.where(np.array(df['RunStatus'])[df.index] == 1, df['ProductionOrder'], np.nan), np.nan)
df['PO_Dummy'] = np.where(df['ProductionOrder'] == 0, np.nan, df['ProductionOrder'])
df['PO_Dummy'].ffill(inplace=True)
df.loc[df.last_valid_index(), 'Production_Order'] = df.loc[df.last_valid_index(), 'ProductionOrder']
df['Production_Order'].bfill(inplace=True)
df['UniquePORun'] = np.where(
np.array(df['POLength'])[np.where(df.index == df.last_valid_index(), df.index, df.index + 1)] <= 0,
np.where(np.array(df['POLength'])[df.index] > 0, np.where(
np.array(df['RunStatus'])[np.where(df.index == df.last_valid_index(), df.index, df.index + 1)] !=
np.array(df['RunStatus'])[df.index], df['ProductionOrder'], np.nan), np.nan),
np.nan)
df['UniquePORun'] = np.where(
np.array(df['Production_Order'])[np.where(df.index == df.last_valid_index(), df.index, df.index + 1)] ==
np.array(df['Production_Order'])[df.index], df['UniquePORun'], df['Production_Order'])
df['Time(mins)'] = 0.5
df['count'] = 1
df['RunNo'] = np.where(pd.isnull(df['UniquePORun']) == False,
df.groupby(['UniquePORun', 'RunCategory'])['count'].cumsum(), np.nan)
df['RunNo'] = np.where(df['RunNo'] == 0.0, np.nan, df['RunNo'])
df['TB'] = np.where(pd.isnull(df['UniquePORun']) == False, df['RunCategory'], df['UniquePORun'])
df['TB'].bfill(inplace=True)
df['RunNo'].bfill(inplace=True)
df['Run_Category'] = np.where(df['TB'] == 1, "Trial ", np.where(df['TB'] == 2, "Bulk ", np.nan))
df['Run_Category'] = df['Run_Category'].astype(str) + df['RunNo'].astype(str)
df.drop(columns=['TB', 'UniquePORun', 'PO_Dummy', 'RunNo', 'count', 'RunCategory', 'ProductionOrder'], inplace=True)
df_Stoppage1 = pd.DataFrame(
list(zip([0, 1, 2, 3, 4, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213],
['Not Selected', 'Line in Fabric', 'Dust', 'Screen Chokeup', 'End of Fabric', 'Pin hole', 'Rod Change', 'Fluff removal', 'Design out setting',
'Screen Wipping', 'Crease /line problem', 'Wider /Uneven width of fabric ( Remove /Apply Tape )',
'Screen Choke up', 'End of fabric', 'Color pump failure', 'Waiting of color', 'Selvedge cut in fabric',
'Improper gum problem in running'])),
columns=['slowSpeedCode', 'Slow_Speed_Reason'])
df_Stoppage0 = pd.DataFrame(
list(zip([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120],
['Not Selected', 'Machine Cleaning', 'Fabric Changeover', 'Trolley/Batch Non-Availability', 'Mechanical Breakdown',
'Electrical Breakdown', 'Preventive Maintenance', 'Low Steam Pressure', 'Low Air Pressure', 'No Water',
'Man Power Shortage', 'No Program', 'Design Change', 'Screen Washing', 'No Color Preparation', 'Design Not Ready',
'Corrective Maintenance', 'Low Temp Due To Thermopack', 'End Ring Not Fit', 'Blanket Coating',
'Gum Circulation Pump Not Working', 'Machine Cleaning', 'Fabric Changeover', 'Trolley/Batch Non-Availability',
'Mechanical Breakdown',
'Electrical Breakdown', 'Preventive Maintenance', 'Low Steam Pressure', 'Low Air Pressure', 'No Water',
'Man Power Shortage', 'No Program', 'Design Change', 'Screen Washing', 'No Color Preparation', 'Design Not Ready',
'Corrective Maintenance', 'Low Temp Due To Thermopack', 'End Ring Not Fit', 'Blanket Coating',
'Gum Circulation Pump Not Working',]))
, columns=['Stoppage', 'Stoppage_Name'])
df=pd.merge(df,df_Stoppage0,on="Stoppage",how="left")
df=pd.merge(df,df_Stoppage1,on="slowSpeedCode",how="left")
return df
def dataframe():
return query_data()
df = dataframe()
app.layout = html.Div([
dcc.Interval(id='refresh', interval=30000),
html.H1("Ichinose-1 IIOT Dashboard",
style={"text-align": "centre", 'display': 'flex', 'justify-content': 'center'}),
html.Div(id="div", children=[]),
html.Br([]),
html.Div([
html.Div([
dcc.DatePickerRange(id="select_date_range",
display_format="MMM D, YYYY",
clearable=True,
number_of_months_shown=4,
style={'width': '100%'},
),
], className="three columns"),
html.Div([
dcc.Dropdown(id="Shift",
options=[
{"label": 'A', "value": 'A'},
{"label": 'B', "value": 'B'},
{"label": 'C', "value": 'C'},
],
multi=False,
# value='A',
placeholder="Select a Shift",
style={'width': '100%'}
),
], className="three columns"),
html.Div([
dcc.Dropdown(id="select_PO",
multi=False,
placeholder="Select a P.O.",
style={'width': '100%'}
),
], className="three columns"),
html.Div([
dcc.Dropdown(id="select_RunCategory",
multi=False,
placeholder="Select Bulk or Trial?",
style={'width': '100%'}
),
], className="three columns"),
], className="row"),
html.Br([]),
html.Br([]),
html.Div([
dcc.RadioItems(
id="Running_Idle_Selector",
options=[
{'label': 'Machine Running ', 'value': 'Running'},
{'label': 'Machine Idle ', 'value': 'Idle'},
{'label': 'Both', 'value': 'Both'}
],
value='Both',
labelStyle={'display': 'inline-block'},
style={
"display": "block",
"position": "relative",
"padding-left": "35px",
"cursor": "pointer",
"font-size": "22px",
"width": "100%",
"height": "2em",
"margin": "10px 0"
},
)
], style={'border': '1px solid black'}),
html.Div([
html.Div([
dcc.Tabs([
dcc.Tab(label="Speed_Length_Graph", children=[
dcc.Graph(id='Speed_Length_Graph', figure={}),
]),
dcc.Tab(label="Air_Water_Graph", children=[
dcc.Graph(id='Air_Water_Graph', figure={}),
]),
dcc.Tab(label="Thermic-Oil_Energy-Consumption_Graph", children=[
dcc.Graph(id='Thermic_Energy_Graph', figure={}),
])
])
], style={'width': '78%', 'border-right': '1px solid black'}),
html.Div([
dcc.Graph(id='piechart'),
], style={'width': '22%'}),
], style={'align-items': 'center', 'border': '1px solid black', 'display': 'flex', 'justify-content': 'center'}),
html.Br([]),
html.Div([
html.Div(id="machine_run_status", children=[], style={
'width': '25%',
'background-color': '#00107a',
'margin': '1px auto',
'color': 'white',
'font-size': 'x-large',
}),
html.Div(id="po_no", children=[], style={
'width': '25%',
'background-color': '#00107a',
'margin': '1px auto',
'color': 'white',
'font-size': 'x-large',
}),
html.Div(id="speed", children=[], style={
'width': '25%',
'background-color': '#00107a',
'margin': '1px auto',
'color': 'white',
'font-size': 'x-large',
}),
html.Div(id="po_length", children=[], style={
'width': '25%',
'background-color': '#00107a',
'margin': '1px auto',
'color': 'white',
'font-size': 'x-large',
}),
], style={
'align-items': 'center',
'display': 'flex',
'justify-content': 'center',
'border': '1px solid black',
}),
html.Br([]),
html.Div([
html.Div([
dcc.Graph(id='Air_Graph', figure={}), ], className="three columns", style={'border': '1px solid black'}),
html.Div([
dcc.Graph(id='Water_Graph', figure={}), ], className="three columns", style={'border': '1px solid black'}),
html.Div([
dcc.Graph(id='Thermic_Graph', figure={}), ], className="three columns",
style={'border': '1px solid black'}),
html.Div([
dcc.Graph(id='Energy_Graph', figure={}), ], className="three columns", style={'border': '1px solid black'})
], className="row"),
html.Br([]),
html.Div([
html.Div([
dcc.Tabs([
dcc.Tab(label="Pareto of Stoppage", children=[
dcc.Graph(id="Pareto_Stoppage", figure={})
]),
dcc.Tab(label="Pareto of Slow Speed", children=[
dcc.Graph(id="Pareto_Slow_Speed", figure={})
])
])
], style={
'width': '78%',
'border-right': '1px solid black'
}),
html.Div([
html.Div(id="stoppage_data_name", children=[], style={
'text-align': 'center',
'background-color': '#03fcd3',
'color': 'black',
'margin': '3px auto',
'font-size': 'xx-large',
'border': '1px solid black',
}),
html.Div([
html.Div(children=['Total Stoppage Time:'], style={
'text-align': 'center',
'background-color': '#ff4242',
'color': 'white',
'margin': '1px auto',
'font-size': 'xx-large',
'border': '1px solid black',
}),
html.Div(id="total_stoppage_time", children=[], style={
'text-align': 'center',
'background-color': '#ff4242',
'color': 'white',
'margin': '1px auto',
'font-size': 'xx-large',
'border': '1px solid black',
}),
], style={
'margin': '10px auto',
'border': '1px solid black',
}),
html.Div([
html.Div(children=['Last Stoppage Time:'], style={
'text-align': 'center',
'background-color': '#ff4242',
'color': 'white',
'margin': '1px auto',
'font-size': 'xx-large',
'border': '1px solid black',
}),
html.Div(id="last_stoppage_time", children=[], style={
'text-align': 'center',
'background-color': '#ff4242',
'color': 'white',
'margin': '1px auto',
'font-size': 'xx-large',
'border': '1px solid black',
}),
], style={
'margin': '10px auto',
'border': '1px solid black',
}),
html.Div([
html.Div(children=['Last Stoppage Name:'], style={
'text-align': 'center',
'background-color': '#ff4242',
'color': 'white',
'margin': '1px auto',
'font-size': 'xx-large',
'border': '1px solid black',
}),
html.Div(id="last_stoppage_name", children=[], style={
'text-align': 'center',
'background-color': '#ff4242',
'color': 'white',
'font-size': 'xx-large',
'margin': '1px auto',
'border': '1px solid black',
}),
], style={
'margin': '10px auto',
'border': '1px solid black',
}),
], style={
'width': '22%',
'position': 'relative'
}),
], style={
'align-items': 'center', 'border': '1px solid black', 'display': 'flex', 'justify-content': 'center'
}),
html.Br([]),
dcc.Tabs([
dcc.Tab(label="Detailed Data", children=[
html.Div([
dash_table.DataTable(
id='datatable',
editable=False,
row_deletable=False,
page_action="native",
page_current=0,
page_size=6,
style_data_conditional=[
{
'if': {'row_index': 'odd'},
'backgroundColor': 'rgb(248, 248, 248)'
}
],
style_header={
'backgroundColor': 'rgb(230, 230, 230)',
'fontWeight': 'bold'
},
style_cell={'textAlign': 'center', 'font_size': '20px', 'font_family': 'noto sans'}
),
], className='row'),
]),
dcc.Tab(label="PO Based Data", children=[
html.Div([
dash_table.DataTable(
id='po_based_data',
editable=False,
row_deletable=False,
page_action="native",
page_current=0,
page_size=6,
style_data_conditional=[
{
'if': {'row_index': 'odd'},
'backgroundColor': 'rgb(248, 248, 248)'
}
],
style_header={
'backgroundColor': 'rgb(230, 230, 230)',
'fontWeight': 'bold'
},
style_cell={'textAlign': 'center', 'font_size': '20px', 'font_family': 'noto sans'}
),
], className='row'),
])
]),
html.Br([]),
html.Br([]),
html.Br([]),
html.Br([]),
html.Br([]),
])