From 3c5d495ffe86a426cd0346885aaeae3bc8d92260 Mon Sep 17 00:00:00 2001
From: Sebastian David <sebastian.david@uni-hamburg.de>
Date: Tue, 16 Nov 2021 10:58:59 +0100
Subject: [PATCH] Fixed a major bug,

where users would see the changes that other users
make, by removing dependency on a global variable.
changed some more variable names and updated code documentation.
---
 ui_programm_fragmente/input_to_checklist.py | 32 ++++++++++++---------
 1 file changed, 19 insertions(+), 13 deletions(-)

diff --git a/ui_programm_fragmente/input_to_checklist.py b/ui_programm_fragmente/input_to_checklist.py
index 0a9fd94..da96b98 100644
--- a/ui_programm_fragmente/input_to_checklist.py
+++ b/ui_programm_fragmente/input_to_checklist.py
@@ -4,10 +4,10 @@ from dash import html
 from dash import callback_context
 from dash.dependencies import Input, Output, State
 from dash.exceptions import PreventUpdate
+import copy
 
 app = dash.Dash(__name__)
 
-list_of_inputs = dict()
 additional_options = ['Update Automatically']
 
 app.layout = html.Div([
@@ -25,7 +25,7 @@ app.layout = html.Div([
     ]),
     # Layer 2: For the checklist and Remove-/Start-Buttons
     html.Div([
-        dcc.Checklist(id='input-checklist',labelStyle = dict(display='block'),value=[]),
+        dcc.Checklist(id='input-checklist',options=[],labelStyle = dict(display='block'),value=[]),
         html.Button(id='clear-all-button',children='Clear All'),
         html.Button(id='clear-selected-button',children='Clear Selected'),
         html.Button(id='start-button',children='Generate Graph')
@@ -54,35 +54,41 @@ input-string is required as Output to clear the input box after each input
     Input('input-string','value'),
     Input('clear-all-button','n_clicks'),
     Input('clear-selected-button','n_clicks'),
+    State('input-checklist','options'),
     State('input-checklist','value')
 )
-def update_input_checklist(input_value,btn1,btn2,all_values):
+def update_input_checklist(input_value,btn1,btn2,all_inputs,selected_inputs):
     '''
         :param input_value: given by dcc.Input
         :type input_value: string
         :param btn1: signals pressing of clear-all-button
         :param btn2: signals pressing of clear-selected-button
-        :param all_values: values of all checked elements
-        :type all_values: list of strings
+        :param all_inputs: all labels and values from the checklist,
+            regardless if they have been checked or not
+        :type all_inputs: list of dictionaries with 2 entries each
+        :param selected_inputs: values of all checked elements
+        :type selected_inputs: list of strings
     '''
     changed_id = [p['prop_id'] for p in callback_context.triggered][0]
     # if clear-all-button was pressed:
     if 'clear-all-button' in changed_id:
-        list_of_inputs.clear()
+        app.layout['input-checklist'].options.clear()
         return list(),list(),''
     # if clear-selected-button was pressed:
     if 'clear-selected-button' in changed_id:
-        for value in all_values:
-            del list_of_inputs[value]
-        return [{'label': i, 'value': i} for i in list_of_inputs],list(),''
+        all_inputs = [i for i in all_inputs if i['value'] not in selected_inputs]
+        return all_inputs,list(),''
     # when the programm is first started:
     if input_value == '':
-        list_of_inputs.clear()
+        app.layout['input-checklist'].options.clear()
         return list(),list(),''
     # when a new element is added via dcc.Input
-    if input_value not in list_of_inputs:
-        list_of_inputs[input_value] = input_value
-    return [{'label': i, 'value': i} for i in list_of_inputs],all_values,''
+    options = copy.copy(app.layout['input-checklist'].options)
+    options = all_inputs
+    currValues = [x['value'] for x in options]
+    if input_value not in currValues:
+        options.append({'label':input_value, 'value':input_value})
+    return options,selected_inputs,''
 
 '''
 This callback shows and hides the (first) help-box
-- 
GitLab