Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	
		[email protected]
		
	commited on
		
		
					Commit 
							
							·
						
						92b1222
	
1
								Parent(s):
							
							3536eb7
								
feat: enhance form handling by updating field values and adding slider input type in config.yaml and form.py
Browse files- config.yaml +9 -12
 - pages/form.py +82 -15
 
    	
        config.yaml
    CHANGED
    
    | 
         @@ -75,13 +75,13 @@ variables: 
     | 
|
| 75 | 
         
             
                    - label: "Spécificités et/ou Certifications"
         
     | 
| 76 | 
         
             
                      nature: 'multiselect'
         
     | 
| 77 | 
         
             
                      key: specificite_certifications
         
     | 
| 78 | 
         
            -
                      options: ["Bio Oui Partielle", "Bio Oui Totale", "HVE (Haute Valeur Environnementale)", "TCS (Techniques Culturales Simplifiées)" 
     | 
| 79 | 
         
            -
                      value: ""
         
     | 
| 80 | 
         | 
| 81 | 
         
             
                    - label: "Date de création de l'entreprise"
         
     | 
| 82 | 
         
             
                      nature: 'date'
         
     | 
| 83 | 
         
             
                      key: date_creation
         
     | 
| 84 | 
         
            -
                      value:  
     | 
| 85 | 
         | 
| 86 | 
         
             
                    - label: "Chiffre d'affaires annuel (en €)"
         
     | 
| 87 | 
         
             
                      nature: 'numeric'
         
     | 
| 
         @@ -103,12 +103,6 @@ variables: 
     | 
|
| 103 | 
         
             
                      key: nb_salaries
         
     | 
| 104 | 
         
             
                      value: 0
         
     | 
| 105 | 
         | 
| 106 | 
         
            -
                    - label: "Tranche effectif"
         
     | 
| 107 | 
         
            -
                      nature: 'selectbox'
         
     | 
| 108 | 
         
            -
                      key: tranche_effectif
         
     | 
| 109 | 
         
            -
                      options: ["1-5", "6-10", "11-20", "21-50", "51-100", "100+"]
         
     | 
| 110 | 
         
            -
                      value: "1-5"
         
     | 
| 111 | 
         
            -
                      
         
     | 
| 112 | 
         
             
                - name: "Type de projet"
         
     | 
| 113 | 
         
             
                  number: 2
         
     | 
| 114 | 
         
             
                  fields:
         
     | 
| 
         @@ -121,12 +115,15 @@ variables: 
     | 
|
| 121 | 
         
             
                      nature: 'selectbox'
         
     | 
| 122 | 
         
             
                      key: projet_categorie
         
     | 
| 123 | 
         
             
                      options: ["Innovation", "Durabilité", "Développement", "Formation"]
         
     | 
| 124 | 
         
            -
                      value:  
     | 
| 125 | 
         | 
| 126 | 
         
             
                    - label: "Budget total estimé"
         
     | 
| 127 | 
         
            -
                      nature: ' 
     | 
| 128 | 
         
             
                      key: projet_budget
         
     | 
| 129 | 
         
            -
                      value:  
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 130 | 
         | 
| 131 | 
         | 
| 132 | 
         
             
                - name: "Critères de Subvention"
         
     | 
| 
         | 
|
| 75 | 
         
             
                    - label: "Spécificités et/ou Certifications"
         
     | 
| 76 | 
         
             
                      nature: 'multiselect'
         
     | 
| 77 | 
         
             
                      key: specificite_certifications
         
     | 
| 78 | 
         
            +
                      options: ["Aucune", "Bio Oui Partielle", "Bio Oui Totale", "HVE (Haute Valeur Environnementale)", "TCS (Techniques Culturales Simplifiées)"]
         
     | 
| 79 | 
         
            +
                      value: ["Aucune"]
         
     | 
| 80 | 
         | 
| 81 | 
         
             
                    - label: "Date de création de l'entreprise"
         
     | 
| 82 | 
         
             
                      nature: 'date'
         
     | 
| 83 | 
         
             
                      key: date_creation
         
     | 
| 84 | 
         
            +
                      value: 
         
     | 
| 85 | 
         | 
| 86 | 
         
             
                    - label: "Chiffre d'affaires annuel (en €)"
         
     | 
| 87 | 
         
             
                      nature: 'numeric'
         
     | 
| 
         | 
|
| 103 | 
         
             
                      key: nb_salaries
         
     | 
| 104 | 
         
             
                      value: 0
         
     | 
| 105 | 
         | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 106 | 
         
             
                - name: "Type de projet"
         
     | 
| 107 | 
         
             
                  number: 2
         
     | 
| 108 | 
         
             
                  fields:
         
     | 
| 
         | 
|
| 115 | 
         
             
                      nature: 'selectbox'
         
     | 
| 116 | 
         
             
                      key: projet_categorie
         
     | 
| 117 | 
         
             
                      options: ["Innovation", "Durabilité", "Développement", "Formation"]
         
     | 
| 118 | 
         
            +
                      value: 
         
     | 
| 119 | 
         | 
| 120 | 
         
             
                    - label: "Budget total estimé"
         
     | 
| 121 | 
         
            +
                      nature: 'slider'
         
     | 
| 122 | 
         
             
                      key: projet_budget
         
     | 
| 123 | 
         
            +
                      value: [] # Valeur par défaut
         
     | 
| 124 | 
         
            +
                      min: 0
         
     | 
| 125 | 
         
            +
                      max: 50000
         
     | 
| 126 | 
         
            +
                      step: 500
         
     | 
| 127 | 
         | 
| 128 | 
         | 
| 129 | 
         
             
                - name: "Critères de Subvention"
         
     | 
    	
        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,80 @@ 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 | 
         
            +
                    default_values = field.get('value', [])
         
     | 
| 66 | 
         
            +
             
     | 
| 67 | 
         
            +
                    st.multiselect(
         
     | 
| 68 | 
         
            +
                        field['label'], 
         
     | 
| 69 | 
         
            +
                        field['options'], 
         
     | 
| 70 | 
         
            +
                        default=default_values if default_values else None, 
         
     | 
| 71 | 
         
            +
                        key=key, 
         
     | 
| 72 | 
         
            +
                        on_change=update_session_state,
         
     | 
| 73 | 
         
            +
                        args=(key,)
         
     | 
| 74 | 
         
            +
                    )
         
     | 
| 75 | 
         
             
                elif field['nature'] == 'date':
         
     | 
| 76 | 
         
            +
                    st.date_input(
         
     | 
| 77 | 
         
            +
                        field['label'], 
         
     | 
| 78 | 
         
            +
                        value=field.get('value', None),
         
     | 
| 79 | 
         
            +
                        key=key, 
         
     | 
| 80 | 
         
            +
                        on_change=update_session_state,
         
     | 
| 81 | 
         
            +
                        args=(key,)
         
     | 
| 82 | 
         
            +
                    )
         
     | 
| 83 | 
         
            +
             
     | 
| 84 | 
         
             
                elif field['nature'] == 'numeric':
         
     | 
| 85 | 
         
            +
                    st.number_input(field['label'], 
         
     | 
| 86 | 
         
            +
                        value=field.get('value', 0), 
         
     | 
| 87 | 
         
            +
                        key=key, 
         
     | 
| 88 | 
         
            +
                        on_change=update_session_state,
         
     | 
| 89 | 
         
            +
                        args=(key,)
         
     | 
| 90 | 
         
            +
                    )
         
     | 
| 91 | 
         
            +
             
     | 
| 92 | 
         
             
                elif field['nature'] == 'text_area':
         
     | 
| 93 | 
         
            +
                    st.text_area(field['label'], 
         
     | 
| 94 | 
         
            +
                        value=field.get('value', ""), 
         
     | 
| 95 | 
         
            +
                        key=key, 
         
     | 
| 96 | 
         
            +
                        on_change=update_session_state,
         
     | 
| 97 | 
         
            +
                        args=(key,)
         
     | 
| 98 | 
         
            +
                    )
         
     | 
| 99 | 
         
            +
             
     | 
| 100 | 
         
            +
                elif field['nature'] == 'slider':
         
     | 
| 101 | 
         
            +
                    st.slider(field['label'], 
         
     | 
| 102 | 
         
            +
                        value=field.get('value', [0, 1000]), 
         
     | 
| 103 | 
         
            +
                        min_value=field.get('min', 0),        # Valeur minimale du slider
         
     | 
| 104 | 
         
            +
                        max_value=field.get('max', 5000),        # Valeur maximale du slider
         
     | 
| 105 | 
         
            +
                        step=field.get('step', 500),
         
     | 
| 106 | 
         
            +
                        key=key, 
         
     | 
| 107 | 
         
            +
                        on_change=update_session_state,
         
     | 
| 108 | 
         
            +
                        args=(key,)
         
     | 
| 109 | 
         
            +
                    )
         
     | 
| 110 | 
         
            +
             
     | 
| 111 | 
         
             
                else:
         
     | 
| 112 | 
         
            +
                    st.text_input(label=field['label'], 
         
     | 
| 113 | 
         
            +
                        value=field.get('value', ""), 
         
     | 
| 114 | 
         
            +
                        key=key, 
         
     | 
| 115 | 
         
            +
                        on_change=update_session_state,
         
     | 
| 116 | 
         
            +
                        args=(key,)
         
     | 
| 117 | 
         
            +
                    )
         
     | 
| 118 | 
         | 
| 119 | 
         
             
            page()
         
     |