Black Lives Matter. Please consider donating to Black Girls Code today.

Hey guys I have a html table in which the new row will be added at Dcc interval. But I have two different tables when I use it in multi page app and single page app? The table from single page is the right one though? Could you guys please take a look?

#Single page app
import dash
from dash.dependencies import Input, Output, State, Event
import dash_core_components as dcc
import dash_html_components as html
import redis

rows = []
r = redis.StrictRedis(host=‘localhost’, port=6379, db=0, decode_responses=True)
app = dash.Dash()

font_size = ‘10.5’

def dynamic_style(value):
if isinstance(value, int):
style = {
‘color’: ‘blue’,
‘font-size’:font_size,
‘font-style’: ‘bold’
}
elif value == ‘–’:
style = {
‘color’: ‘red’,
‘font-size’:font_size
}
else:
style = {
‘color’: ‘blue’,
‘font-size’:font_size,
‘background’: ‘white’,
‘text-align’: ‘center’,
‘font-face’: ‘sans-serif’
}
return style

app.layout = html.Div([
html.H2(‘Monitoring’),
html.Br(),
html.Br(),
html.H4(id=‘log’, style={‘color’: ‘black’}),
html.Div(id=‘monitor-table’),
dcc.Interval(
id=‘interval-timer’,
interval=2*1000,
n_intervals=0
)
])
@app.callback(
Output(‘monitor-table’, ‘children’),
[Input(‘interval-timer’, ‘n_intervals’)])
def update_table(n):
pos = r.hgetall(‘SamplePositions’)
sample_pos = []
for p in pos.values():
sample_pos.append§
#Header of the tabele
header_table = r.lrange(‘sample-position-headers’, 0, -1)
header_table.insert(0, ‘Time’)
if len(rows)> 10:
rows[0:2] =[]
for i in range(0,n,5):
f = []
for h in header_table:
f.append(’–’)
f[0]= n
for i in sample_pos:
if i in header_table:
ind = header_table.index(i)
value = []
for k,v in pos.items():
if i == v:
if i == ‘tray’:
value.append(k)
else:
value = k
if ind == 1:
f[ind] = len(value)
else:
f[ind] = value
row = html.Tr([html.Td(col) for col in f])
rows.append(row)
def generate_table(max_rows=10):
return html.Table(
# Header
[html.Tr([html.Th(col) for col in header_table])] +
# Body
rows
)
return generate_table()

if name == ‘main’:
app.run_server(debug=True)

#Multi page app
from dash.dependencies import Input, Output, State, Event
import dash_core_components as dcc
import dash_html_components as html
from app import r, app
rows = []
positions = [‘Time’, ‘tray’, ‘sg#0’, ‘linx#0’, ‘linx#1’, ‘imag#1’, ‘linx#3’, ‘reject’, ‘disk#0’, ‘disk#1’, ‘disk#2’, ‘disk#3’, ‘disk#4’,
‘caro#0’, ‘caro#1’, ‘imag#2’, ‘caro#2’, ‘getcas’, ‘caro#3’, ‘clamp’, ‘caro#4’, ‘caro#5’, ‘eject’,‘output’]
layout = html.Div([
html.H2(‘Monitoring’),
html.Br(),
html.Br(),
html.Label('Whats happening inside: '),
html.H4(id=‘log’, style={‘color’: ‘black’}),
dcc.Interval(
id=‘interval-timer’,
interval=2 * 500,
n_intervals=0
),
html.Div(id=‘monitor-table’),
])

@app.callback(
Output(‘monitor-table’, ‘children’),
[Input(‘interval-timer’, ‘n_intervals’)])
def update_table(n):
for j in range(0,n,5):
pos = r.hgetall(‘SamplePositions’)
sample_pos = []
for p in pos.values():
sample_pos.append§
f = []
for i in positions:
f.append(’–’)
f[0] = j
for i in sample_pos:
if i in positions:
ind = positions.index(i)
f[ind]= ‘s1’
row = html.Tr([html.Td(col) for col in f])
rows.append(row)
def generate_table(max_rows=10):
return html.Table(
# Header
[html.Tr([html.Th(col) for col in positions])] +
# Body
rows
)
return generate_table()