Commit
·
97ab350
1
Parent(s):
2e648fc
Fix bug on form
Browse files- config.yaml +3 -3
- pages/form.py +70 -16
config.yaml
CHANGED
|
@@ -28,13 +28,13 @@ variables:
|
|
| 28 |
- label : Millésime
|
| 29 |
nature: 'selectbox'
|
| 30 |
key : param2
|
| 31 |
-
options : ["2019", "2020", "2021"]
|
| 32 |
value: "2019"
|
| 33 |
- label : Indicateur
|
| 34 |
-
nature: '
|
| 35 |
key : param3
|
| 36 |
options : ["Coût de revient", "Prix de revient", "Prix de revient par hectare", "Prix de revient par hectolitre", "Coût de production", "Prix de vente moyen vrac", "Prix de vente moyen à la bouteille", "Main d'oeuvre", "Cours du jour", "Hectare de vigne en production"]
|
| 37 |
-
value: "
|
| 38 |
|
| 39 |
prompt_system: "
|
| 40 |
Tu es un système expert sur le pilotage des données d'une exploitation viticole.
|
|
|
|
| 28 |
- label : Millésime
|
| 29 |
nature: 'selectbox'
|
| 30 |
key : param2
|
| 31 |
+
options : ["2019", "2020", "2021", "2022", "2023", "2024"]
|
| 32 |
value: "2019"
|
| 33 |
- label : Indicateur
|
| 34 |
+
nature: 'selectbox'
|
| 35 |
key : param3
|
| 36 |
options : ["Coût de revient", "Prix de revient", "Prix de revient par hectare", "Prix de revient par hectolitre", "Coût de production", "Prix de vente moyen vrac", "Prix de vente moyen à la bouteille", "Main d'oeuvre", "Cours du jour", "Hectare de vigne en production"]
|
| 37 |
+
value: "Coût de revient"
|
| 38 |
|
| 39 |
prompt_system: "
|
| 40 |
Tu es un système expert sur le pilotage des données d'une exploitation viticole.
|
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 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
elif field['nature'] == 'selectbox':
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
elif field['nature'] == 'multiselect':
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
elif field['nature'] == 'date':
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
elif field['nature'] == 'numeric':
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
elif field['nature'] == 'text_area':
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
else:
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|