mirror of
https://gitea.psi.ch/APOG/acsmnode.git
synced 2025-06-26 19:41:12 +02:00
Implemented commit flag button logic, which involves also displaying recorded flags on table.
This commit is contained in:
@ -14,19 +14,21 @@ sys.path.append(os.path.join(root_dir,'dima'))
|
|||||||
|
|
||||||
import dima.src.hdf5_data_extraction as h5de
|
import dima.src.hdf5_data_extraction as h5de
|
||||||
import dima.src.metadata_review_lib as ma
|
import dima.src.metadata_review_lib as ma
|
||||||
import dima.src.g5505_utils as utils
|
import dima.utils.g5505_utils as utils
|
||||||
|
|
||||||
|
|
||||||
import data_flagging_utils as data_flagging_utils
|
import data_flagging_utils as data_flagging_utils
|
||||||
|
|
||||||
from dash import Dash, html, dcc, callback, Output, Input, State
|
from dash import Dash, html, dcc, callback, Output, Input, State, dash_table
|
||||||
import plotly.graph_objs as go
|
import plotly.graph_objs as go
|
||||||
from plotly.subplots import make_subplots
|
from plotly.subplots import make_subplots
|
||||||
import dash_bootstrap_components as dbc
|
import dash_bootstrap_components as dbc
|
||||||
|
import json
|
||||||
# Initialize Dash app with Bootstrap theme
|
# Initialize Dash app with Bootstrap theme
|
||||||
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
||||||
|
|
||||||
|
#df = pd.DataFrame.empty()
|
||||||
|
|
||||||
app.layout = dbc.Container([
|
app.layout = dbc.Container([
|
||||||
dbc.Row([
|
dbc.Row([
|
||||||
|
|
||||||
@ -60,9 +62,13 @@ app.layout = dbc.Container([
|
|||||||
|
|
||||||
dbc.Row([
|
dbc.Row([
|
||||||
dbc.Col([ dcc.Graph(id='timeseries-plot')], width=8),
|
dbc.Col([ dcc.Graph(id='timeseries-plot')], width=8),
|
||||||
dbc.Col([html.Div(id='flag-record', style={'whiteSpace': 'pre-line'})], width=4), #config={'modeBarButtons': True,
|
#dbc.Col([html.Div(id='flag-record', style={'whiteSpace': 'pre-line'})], width=4), #config={'modeBarButtons': True,
|
||||||
#'modeBarButtonsToAdd':['select2d','lasso2d'],
|
#'modeBarButtonsToAdd':['select2d','lasso2d'],
|
||||||
#'modeBarButtonsToRemove': ['zoom', 'pan']}),], width=12)
|
#'modeBarButtonsToRemove': ['zoom', 'pan']}),], width=12)
|
||||||
|
dbc.Col([dash_table.DataTable(data=[],
|
||||||
|
columns=[{"name": i, "id": i} for i in ['id','startdate','enddate','value']],
|
||||||
|
id='tbl',
|
||||||
|
style_header={'textAlign': 'center'},)], width=4)
|
||||||
|
|
||||||
],justify="center", align="center"),
|
],justify="center", align="center"),
|
||||||
|
|
||||||
@ -102,7 +108,7 @@ def load_data(filename, contents):
|
|||||||
|
|
||||||
@app.callback(
|
@app.callback(
|
||||||
Output('timeseries-plot', 'figure', allow_duplicate=True),
|
Output('timeseries-plot', 'figure', allow_duplicate=True),
|
||||||
Output('textarea-example-output','children'),
|
#Output('textarea-example-output','children'),
|
||||||
Input('flag-button', 'n_clicks'),
|
Input('flag-button', 'n_clicks'),
|
||||||
State('timeseries-plot', 'figure'),
|
State('timeseries-plot', 'figure'),
|
||||||
State('memory-output', 'data'),
|
State('memory-output', 'data'),
|
||||||
@ -117,7 +123,8 @@ def create_flag(n_clicks, fig, data):
|
|||||||
'doubleClick' : 'reset'
|
'doubleClick' : 'reset'
|
||||||
})
|
})
|
||||||
value = '{} amigos'.format(n_clicks)
|
value = '{} amigos'.format(n_clicks)
|
||||||
return fig, f'You have entered: \n{value}'
|
#return fig, f'You have entered: \n{value}'
|
||||||
|
return fig
|
||||||
|
|
||||||
#@app.callback(
|
#@app.callback(
|
||||||
# Output('timeseries-plot', 'figure', allow_duplicate=True),
|
# Output('timeseries-plot', 'figure', allow_duplicate=True),
|
||||||
@ -180,15 +187,74 @@ def clear_flag(n_clicks, fig, data):
|
|||||||
else:
|
else:
|
||||||
return dash.no_update, dash.no_update
|
return dash.no_update, dash.no_update
|
||||||
|
|
||||||
@callback(Output('flag-record', 'children'),
|
@callback(Output('tbl', 'data'),
|
||||||
Input('commit-flag-button','n_clicks'),
|
Input('commit-flag-button','n_clicks'),
|
||||||
|
State('flag-options','value'),
|
||||||
State('timeseries-plot','selectedData'),
|
State('timeseries-plot','selectedData'),
|
||||||
prevent_initial_call=True)
|
prevent_initial_call=True)
|
||||||
|
|
||||||
def commit_flag(n_clicks,selected_Data):
|
|
||||||
|
def commit_flag(n_clicks,flag_value,selected_Data):
|
||||||
|
|
||||||
value = selected_Data
|
value = selected_Data
|
||||||
return f'You have entered: \n{value}'
|
if (selected_Data is None) and (not isinstance(selected_Data,dict)):
|
||||||
|
return []
|
||||||
|
elif not selected_Data.get('range',[]): # verify if there is a flag's time interval to commit
|
||||||
|
return []
|
||||||
|
|
||||||
|
# TODO: modify the name path/to/name to reflect the directory provenance
|
||||||
|
flagfolderpath = 'flags/'
|
||||||
|
|
||||||
|
if not os.path.isdir(flagfolderpath):
|
||||||
|
os.makedirs(flagfolderpath)
|
||||||
|
|
||||||
|
#dirlist = os.listdir(flagfolderpath)
|
||||||
|
# Get all files in the directory with their full paths
|
||||||
|
files = [os.path.join(flagfolderpath, f) for f in os.listdir(flagfolderpath)]
|
||||||
|
|
||||||
|
# Sort files by creation time
|
||||||
|
dirlist_sorted_by_creation = sorted(files, key=os.path.getctime)
|
||||||
|
|
||||||
|
#dirlist = dirlist.sort(key=lambda x: int(x.split('_')[1].split('.')[0]))
|
||||||
|
|
||||||
|
display_flag_registry = True
|
||||||
|
if not display_flag_registry:
|
||||||
|
data = []
|
||||||
|
else:
|
||||||
|
data = []
|
||||||
|
for pathtofile in dirlist_sorted_by_creation:
|
||||||
|
if '.json' in pathtofile:
|
||||||
|
with open(pathtofile,'r') as f:
|
||||||
|
data.append(json.load(f))
|
||||||
|
|
||||||
|
number_of_existing_flags = len(dirlist_sorted_by_creation)
|
||||||
|
flagid = number_of_existing_flags+1
|
||||||
|
flag_filename = os.path.join(flagfolderpath,f'flag_{flagid}.json')
|
||||||
|
|
||||||
|
#if not os.path.exists(flag_filename):
|
||||||
|
# with open(flag_filename,'r') as open_flagsfile:
|
||||||
|
# json_flagsobject = json.load(open_flagsfile)
|
||||||
|
# data = [json_flagsobject[key] for key in json_flagsobject.keys()]
|
||||||
|
|
||||||
|
|
||||||
|
#return f'You have entered: \n{value}'
|
||||||
|
for key, value in selected_Data['range'].items():
|
||||||
|
if 'x' in key:
|
||||||
|
new_row = {'id':flagid,'startdate':value[0],'enddate':value[1],'value': flag_value}
|
||||||
|
data.append(new_row)
|
||||||
|
#data = [{'startdate':value[0],'enddate':value[1],'value':90}]
|
||||||
|
|
||||||
|
if not os.path.exists(flag_filename):
|
||||||
|
with open(flag_filename,'w') as flagsfile:
|
||||||
|
#json_flagsobject = json.dump({'row'+str(len(data)): new_row}, flagsfile)
|
||||||
|
json.dump(new_row, flagsfile)
|
||||||
|
#else:
|
||||||
|
# with open(flag_filename,'a') as flagsfile:
|
||||||
|
# json.dump(new_row, flagsfile)
|
||||||
|
#json.dump({'row'+str(len(data)): new_row}, flagsfile)
|
||||||
|
#data = [json_flagsobject[key] for key in json_flagsobject.keys()]
|
||||||
|
|
||||||
|
return data
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run_server(debug=True)
|
app.run_server(debug=True)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user