cd@bziiit.com commited on
Commit
673831c
·
1 Parent(s): 8b93442

Fix form value to session storage

Browse files
Files changed (1) hide show
  1. pages/form.py +69 -15
pages/form.py CHANGED
@@ -1,6 +1,17 @@
1
  import streamlit as st
2
  from util import getYamlConfig
3
 
 
 
 
 
 
 
 
 
 
 
 
4
  def page():
5
  st.subheader("Définissez vos paramètres")
6
 
@@ -18,7 +29,9 @@ def page():
18
  for part, tab in zip(parts_sorted, tabs):
19
  with tab:
20
  for field in part['fields']:
21
- display_field(field)
 
 
22
  else:
23
  # Display fields directly if no parts are defined
24
  for field in st.session_state.data_dict:
@@ -27,26 +40,67 @@ def page():
27
 
28
  def display_field(field):
29
  """Helper function to create the correct input based on field 'nature'."""
 
30
  if field['nature'] == 'radio':
31
- value = st.radio(field['label'], field['options'], key=field['key'])
32
- field['value'] = value
 
 
 
 
 
 
 
33
  elif field['nature'] == 'selectbox':
34
- value = st.selectbox(field['label'], field['options'], key=field['key'])
35
- field['value'] = value
 
 
 
 
 
 
 
36
  elif field['nature'] == 'multiselect':
37
- value = st.multiselect(field['label'], field['options'], key=field['key'])
38
- field['value'] = value
 
 
 
 
 
 
39
  elif field['nature'] == 'date':
40
- value = st.date_input(field['label'], key=field['key'])
41
- field['value'] = value
 
 
 
 
 
 
42
  elif field['nature'] == 'numeric':
43
- value = st.number_input(field['label'], key=field['key'])
44
- field['value'] = value
 
 
 
 
 
45
  elif field['nature'] == 'text_area':
46
- value = st.text_area(field['label'], value=field['value'] if field['value'] else "", key=field['key'])
47
- field['value'] = value
 
 
 
 
 
48
  else:
49
- value = st.text_input(label=field['label'], value=field['value'] if field['value'] else "")
50
- field['value'] = value
 
 
 
 
51
 
52
  page()
 
1
  import streamlit as st
2
  from util import getYamlConfig
3
 
4
+ def update_session_state(key,):
5
+ # Get new value from session state and change key for save it in params
6
+ new_value = st.session_state[key]
7
+ key = key[5:]
8
+
9
+ for item in st.session_state.data_dict:
10
+ if item['key'] == key:
11
+ item['value'] = new_value
12
+ break
13
+
14
+
15
  def page():
16
  st.subheader("Définissez vos paramètres")
17
 
 
29
  for part, tab in zip(parts_sorted, tabs):
30
  with tab:
31
  for field in part['fields']:
32
+ for field_session in st.session_state.data_dict:
33
+ if field['key'] == field_session['key']:
34
+ display_field(field_session)
35
  else:
36
  # Display fields directly if no parts are defined
37
  for field in st.session_state.data_dict:
 
40
 
41
  def display_field(field):
42
  """Helper function to create the correct input based on field 'nature'."""
43
+ key = 'form_' + field['key']
44
  if field['nature'] == 'radio':
45
+ st.radio(
46
+ field['label'],
47
+ field['options'],
48
+ index=field['options'].index(field.get('value')) if field.get('value') in field['options'] else 0,
49
+ key=key,
50
+ on_change=update_session_state,
51
+ args=(key,)
52
+ )
53
+
54
  elif field['nature'] == 'selectbox':
55
+ st.selectbox(
56
+ field['label'],
57
+ field['options'],
58
+ index=field['options'].index(field.get('value')) if field.get('value') in field['options'] else 0,
59
+ key=key,
60
+ on_change=update_session_state,
61
+ args=(key,)
62
+ )
63
+
64
  elif field['nature'] == 'multiselect':
65
+ st.multiselect(
66
+ field['label'],
67
+ field['options'],
68
+ default=[field['options'].index(value) for value in field.get('value') if value in field['options']],
69
+ key=key,
70
+ on_change=update_session_state,
71
+ args=(key,)
72
+ )
73
  elif field['nature'] == 'date':
74
+ st.date_input(
75
+ field['label'],
76
+ value=field.get('value', None),
77
+ key=key,
78
+ on_change=update_session_state,
79
+ args=(key,)
80
+ )
81
+
82
  elif field['nature'] == 'numeric':
83
+ st.number_input(field['label'],
84
+ value=field.get('value', 0),
85
+ key=key,
86
+ on_change=update_session_state,
87
+ args=(key,)
88
+ )
89
+
90
  elif field['nature'] == 'text_area':
91
+ st.text_area(field['label'],
92
+ value=field.get('value', ""),
93
+ key=key,
94
+ on_change=update_session_state,
95
+ args=(key,)
96
+ )
97
+
98
  else:
99
+ st.text_input(label=field['label'],
100
+ value=field.get('value', ""),
101
+ key=key,
102
+ on_change=update_session_state,
103
+ args=(key,)
104
+ )
105
 
106
  page()