pjgerrits commited on
Commit
d5ff9e4
1 Parent(s): 7635eb3

update marker control

Browse files
Files changed (1) hide show
  1. app.py +65 -50
app.py CHANGED
@@ -8,7 +8,7 @@ import pandas as pd
8
  st.set_page_config(layout="wide")
9
 
10
  # Database connection
11
- @st.cache(allow_output_mutation=True)
12
  def connect_to_db():
13
  return psycopg2.connect(
14
  dbname="glprui_jloddr",
@@ -57,6 +57,8 @@ def submit_data(age, gender, transport, multi_transport, time_of_day, day_of_wee
57
  # Initialize session state for points
58
  if 'points' not in st.session_state:
59
  st.session_state['points'] = {'start': None, 'lost': None, 'end': None}
 
 
60
 
61
  # Sidebar for marker management
62
  st.sidebar.title("Step 1: Add Markers on the Map")
@@ -67,73 +69,86 @@ point_type = st.sidebar.radio("Select Point Type to Add", ["start", "lost", "end
67
  # Buttons to clear points
68
  if st.sidebar.button("Clear All Markers"):
69
  st.session_state['points'] = {'start': None, 'lost': None, 'end': None}
70
- st.experimental_rerun()
71
 
72
  if st.sidebar.button("Clear Selected Marker"):
73
  st.session_state['points'][point_type] = None
74
- st.experimental_rerun()
75
-
76
- # Initialize map
77
- m = folium.Map(location=[51.5074, -0.1278], zoom_start=10, control_scale=True)
78
-
79
- # Add existing points to the map
80
- for pointType, coords in st.session_state['points'].items():
81
- if coords:
82
- folium.Marker(
83
- location=coords,
84
- popup=pointType,
85
- icon=folium.Icon(color="red" if pointType == "start" else "blue" if pointType == "end" else "orange")
86
- ).add_to(m)
87
-
88
- # Add click functionality without creating popups
89
- draw = Draw(
90
- export=False,
91
- draw_options={
92
- "circle": False,
93
- "polyline": False,
94
- "polygon": False,
95
- "rectangle": False,
96
- "circlemarker": False,
97
- "marker": True,
98
- },
99
- edit_options={"edit": False}
100
- )
101
- draw.add_to(m)
102
- Geocoder(add_marker=True).add_to(m)
103
- MiniMap().add_to(m)
 
 
 
 
 
 
104
 
105
  # Display map
106
- output = st_folium(m, width="100%", height=600, key="map")
 
107
 
108
  # Check for map click data
109
  if output and 'last_clicked' in output and output['last_clicked'] is not None:
110
  lat = output['last_clicked']['lat']
111
  lon = output['last_clicked']['lng']
112
  st.session_state['points'][point_type] = (lat, lon)
113
- st.experimental_rerun()
114
 
115
  # Display current points
116
  st.write("Current Points:", st.session_state['points'])
117
 
118
  # Step 2: Survey questions
119
- st.sidebar.title("Step 2: Fill Out the Survey")
 
 
 
 
 
 
120
 
121
- age = st.sidebar.selectbox("Age", ["0-10", "11-20", "21-30", "31-40", "41-50", "51-60", "61-70", "71-80", "81-90", "91-100"])
122
- gender = st.sidebar.radio("Gender", ["M", "F", "O", "PNTS"])
123
- transport = st.sidebar.radio("Mode of Transport", ["Walk", "Car", "Bike", "Train", "Other", "Multi"])
124
 
125
- multi_transport = []
126
- if transport == "Multi":
127
- multi_transport = st.sidebar.multiselect("If Multi, Select Modes Used", ["Walk", "Car", "Bike", "Train", "Other"])
128
 
129
- time_of_day = st.sidebar.selectbox("Time of Day", ["Morning", "Afternoon", "Evening", "Night"])
130
- day_of_week = st.sidebar.selectbox("Day of the Week", ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"])
131
- description = st.sidebar.text_area("Why did you get lost?")
132
 
133
- if st.sidebar.button("Save"):
134
- submit_data(age, gender, transport, multi_transport, time_of_day, day_of_week, description, st.session_state['points'])
135
 
136
- st.sidebar.markdown("---")
137
- st.sidebar.markdown("For a more detailed survey, click the link or scan the QR code:")
138
- st.sidebar.markdown("[https://arcg.is/1GK5jP0](https://arcg.is/1GK5jP0)")
139
- st.sidebar.image("static/Getting Lost Survey.png", width=200)
 
8
  st.set_page_config(layout="wide")
9
 
10
  # Database connection
11
+ @st.cache_resource
12
  def connect_to_db():
13
  return psycopg2.connect(
14
  dbname="glprui_jloddr",
 
57
  # Initialize session state for points
58
  if 'points' not in st.session_state:
59
  st.session_state['points'] = {'start': None, 'lost': None, 'end': None}
60
+ if 'step' not in st.session_state:
61
+ st.session_state['step'] = 1
62
 
63
  # Sidebar for marker management
64
  st.sidebar.title("Step 1: Add Markers on the Map")
 
69
  # Buttons to clear points
70
  if st.sidebar.button("Clear All Markers"):
71
  st.session_state['points'] = {'start': None, 'lost': None, 'end': None}
72
+ st.rerun()
73
 
74
  if st.sidebar.button("Clear Selected Marker"):
75
  st.session_state['points'][point_type] = None
76
+ st.rerun()
77
+
78
+ # Create a placeholder for the map
79
+ map_placeholder = st.empty()
80
+
81
+ def create_map():
82
+ # Initialize map
83
+ m = folium.Map(location=[51.5074, -0.1278], zoom_start=10, control_scale=True)
84
+
85
+ # Add existing points to the map
86
+ for pointType, coords in st.session_state['points'].items():
87
+ if coords:
88
+ folium.Marker(
89
+ location=coords,
90
+ popup=pointType,
91
+ icon=folium.Icon(color="red" if pointType == "start" else "blue" if pointType == "end" else "orange")
92
+ ).add_to(m)
93
+
94
+ # Add click functionality without creating popups
95
+ draw = Draw(
96
+ export=False,
97
+ draw_options={
98
+ "circle": False,
99
+ "polyline": False,
100
+ "polygon": False,
101
+ "rectangle": False,
102
+ "circlemarker": False,
103
+ "marker": True,
104
+ },
105
+ edit_options={"edit": False}
106
+ )
107
+ draw.add_to(m)
108
+ Geocoder(add_marker=True).add_to(m)
109
+ MiniMap().add_to(m)
110
+
111
+ return m
112
 
113
  # Display map
114
+ with map_placeholder.container():
115
+ output = st_folium(create_map(), width="100%", height=600, key="map")
116
 
117
  # Check for map click data
118
  if output and 'last_clicked' in output and output['last_clicked'] is not None:
119
  lat = output['last_clicked']['lat']
120
  lon = output['last_clicked']['lng']
121
  st.session_state['points'][point_type] = (lat, lon)
122
+ st.rerun()
123
 
124
  # Display current points
125
  st.write("Current Points:", st.session_state['points'])
126
 
127
  # Step 2: Survey questions
128
+ if st.session_state['step'] == 1:
129
+ if st.sidebar.button("Done Adding Markers"):
130
+ st.session_state['step'] = 2
131
+ st.rerun()
132
+
133
+ if st.session_state['step'] == 2:
134
+ st.sidebar.title("Step 2: Fill Out the Survey")
135
 
136
+ age = st.sidebar.selectbox("Age", ["0-10", "11-20", "21-30", "31-40", "41-50", "51-60", "61-70", "71-80", "81-90", "91-100"])
137
+ gender = st.sidebar.radio("Gender", ["M", "F", "O", "PNTS"])
138
+ transport = st.sidebar.radio("Mode of Transport", ["Walk", "Car", "Bike", "Train", "Other", "Multi"])
139
 
140
+ multi_transport = []
141
+ if transport == "Multi":
142
+ multi_transport = st.sidebar.multiselect("If Multi, Select Modes Used", ["Walk", "Car", "Bike", "Train", "Other"])
143
 
144
+ time_of_day = st.sidebar.selectbox("Time of Day", ["Morning", "Afternoon", "Evening", "Night"])
145
+ day_of_week = st.sidebar.selectbox("Day of the Week", ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"])
146
+ description = st.sidebar.text_area("Why did you get lost?")
147
 
148
+ if st.sidebar.button("Save"):
149
+ submit_data(age, gender, transport, multi_transport, time_of_day, day_of_week, description, st.session_state['points'])
150
 
151
+ st.sidebar.markdown("---")
152
+ st.sidebar.markdown("For a more detailed survey, click the link or scan the QR code:")
153
+ st.sidebar.markdown("[https://arcg.is/1GK5jP0](https://arcg.is/1GK5jP0)")
154
+ st.sidebar.image("static/Getting Lost Survey.png", width=200)