mmh, looks nice but I don’t see how to manage dmc notification that way (progress bar will not be suitable for me because the long run is an external function).
Do you have an example of Notification using Interval ? I am stuck with an error at execution because enrich seems to do some modification in input/output, and i’m not able to return a dictionnary in data_store…
from dash import dcc, callback_context as ctx, no_update, callback
from dash.exceptions import PreventUpdate
from dash_extensions.enrich import Output, Input, State, html, DashProxy, LogTransform, DashLogger, MultiplexerTransform
import dash_mantine_components as dmc
from dash_iconify import DashIconify
import time
app = DashProxy(transforms=[MultiplexerTransform(), LogTransform()])
app.layout = html.Div([
html.Button("Run", id="btn"),
html.Button("Long", id="long"),
html.Div(id="txt"),
dcc.Store(id='notif_store'),
dcc.Interval(
id='interval-component',
interval=1 * 1000, # in milliseconds
n_intervals=0,
disabled=False
)
])
@app.callback(Output("txt", "children"), Input("btn", "n_clicks"), log=True)
def do_stuff(n_clicks, dash_logger: DashLogger):
if not n_clicks:
raise PreventUpdate()
dash_logger.info("Here goes some info")
dash_logger.warning("This is a warning")
dash_logger.error("Some error occurred")
return f"Run number {n_clicks} completed"
@app.callback(Output("txt", "children"), Input("long", "n_clicks"), log=True)
def do_stuff(n_clicks, dash_logger: DashLogger):
if not n_clicks:
raise PreventUpdate()
write_notification("work in progress", level='info', loading=True)
time.sleep(10)
write_notification("done", level='info', loading=False)
return f"Long Run number {n_clicks} completed"
@app.callback(
Output("notif_store", "data"),
[Input('interval-component', 'n_intervals')],
[State('notif_store','data')],
log=True,
)
def update_notif(n,store,dash_logger: DashLogger):
if not ctx.triggered:
return no_update
level, loading, message = read_notification()
icomp = 0
print(store)
if store is not None:
if 'level' in store:
if store['level'] == level:
icomp = icomp+1
if 'loading' in store:
if store['loading'] == loading:
icomp = icomp + 1
if 'message' in store:
if store['message'] == message:
icomp = icomp + 1
else:
store=dict()
if icomp < 3:
if level == "info":
dash_logger.info(message, loading=loading)
store['level']=level
store['loading'] = loading
store['message'] = message
print(store)
return store
def write_notification(message,level='info',loading=False):
with open('notif_log.txt', 'w') as f:
f.write('{};{};{}'.format(level,loading,message))
def read_notification():
filename = 'notif_log.txt'
with open(filename, 'r') as f:
lines = f.readlines()
last_notif = lines[-1].split(';')
return last_notif[0], last_notif[1], last_notif[2]
if __name__ == '__main__':
app.run_server(debug=True)