pjgerrits commited on
Commit
2b9fdf3
1 Parent(s): 2444a8b

updates to streamlit app

Browse files
Files changed (2) hide show
  1. app.py +58 -15
  2. requirements.txt +2 -0
app.py CHANGED
@@ -1,8 +1,9 @@
1
  import streamlit as st
2
  import psycopg2
3
  import pandas as pd
 
4
 
5
- st.title("Getting Lost Survey")
6
 
7
  # Database connection
8
  @st.cache(allow_output_mutation=True)
@@ -17,7 +18,7 @@ def connect_to_db():
17
  )
18
 
19
  # Function to submit data
20
- def submit_data(age, gender, transport, multi_transport, time_of_day, day_of_week, description, start_point, lost_point, end_point):
21
  conn = connect_to_db()
22
  cursor = conn.cursor()
23
 
@@ -32,7 +33,6 @@ def submit_data(age, gender, transport, multi_transport, time_of_day, day_of_wee
32
 
33
  record_id = cursor.fetchone()[0]
34
 
35
- points = {'start': start_point, 'lost': lost_point, 'end': end_point}
36
  for pointType, point in points.items():
37
  if point:
38
  cursor.execute(
@@ -52,25 +52,68 @@ def submit_data(age, gender, transport, multi_transport, time_of_day, day_of_wee
52
  cursor.close()
53
  conn.close()
54
 
55
- # Form inputs
56
- age = st.selectbox("Age", ["0-10", "11-20", "21-30", "31-40", "41-50", "51-60", "61-70", "71-80", "81-90", "91-100"])
57
- gender = st.radio("Gender", ["M", "F", "O", "PNTS"])
58
- transport = st.radio("Mode of Transport", ["Walk", "Car", "Bike", "Train", "Other", "Multi"])
 
 
59
 
60
  multi_transport = []
61
  if transport == "Multi":
62
- multi_transport = st.multiselect("If Multi, Select Modes Used", ["Walk", "Car", "Bike", "Train", "Other"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
- time_of_day = st.selectbox("Time of Day", ["Morning", "Afternoon", "Evening", "Night"])
65
- day_of_week = st.selectbox("Day of the Week", ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"])
66
- description = st.text_area("Why did you get lost?")
 
 
 
67
 
68
- start_point = st.map()
69
- lost_point = st.map()
70
- end_point = st.map()
71
 
72
  if st.button("Save"):
73
- submit_data(age, gender, transport, multi_transport, time_of_day, day_of_week, description, start_point, lost_point, end_point)
74
 
75
  st.markdown("---")
76
  st.markdown("For a more detailed survey, click the link or scan the QR code:")
 
1
  import streamlit as st
2
  import psycopg2
3
  import pandas as pd
4
+ import pydeck as pdk
5
 
6
+ st.set_page_config(layout="wide")
7
 
8
  # Database connection
9
  @st.cache(allow_output_mutation=True)
 
18
  )
19
 
20
  # Function to submit data
21
+ def submit_data(age, gender, transport, multi_transport, time_of_day, day_of_week, description, points):
22
  conn = connect_to_db()
23
  cursor = conn.cursor()
24
 
 
33
 
34
  record_id = cursor.fetchone()[0]
35
 
 
36
  for pointType, point in points.items():
37
  if point:
38
  cursor.execute(
 
52
  cursor.close()
53
  conn.close()
54
 
55
+ # Sidebar form inputs
56
+ st.sidebar.title("Getting Lost Survey")
57
+
58
+ 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"])
59
+ gender = st.sidebar.radio("Gender", ["M", "F", "O", "PNTS"])
60
+ transport = st.sidebar.radio("Mode of Transport", ["Walk", "Car", "Bike", "Train", "Other", "Multi"])
61
 
62
  multi_transport = []
63
  if transport == "Multi":
64
+ multi_transport = st.sidebar.multiselect("If Multi, Select Modes Used", ["Walk", "Car", "Bike", "Train", "Other"])
65
+
66
+ time_of_day = st.sidebar.selectbox("Time of Day", ["Morning", "Afternoon", "Evening", "Night"])
67
+ day_of_week = st.sidebar.selectbox("Day of the Week", ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"])
68
+ description = st.sidebar.text_area("Why did you get lost?")
69
+
70
+ # Map interaction
71
+ st.sidebar.title("Select Points on Map")
72
+ point_type = st.sidebar.radio("Select Point Type", ["start", "lost", "end"])
73
+
74
+ if 'points' not in st.session_state:
75
+ st.session_state['points'] = {'start': None, 'lost': None, 'end': None}
76
+
77
+ def map_click(lat, lon):
78
+ st.session_state['points'][point_type] = (lat, lon)
79
+
80
+ st.sidebar.write("Click on the map to select points.")
81
+
82
+ # Map display
83
+ map_data = pd.DataFrame([
84
+ {'lat': st.session_state['points']['start'][0], 'lon': st.session_state['points']['start'][1]} if st.session_state['points']['start'] else {'lat': None, 'lon': None},
85
+ {'lat': st.session_state['points']['lost'][0], 'lon': st.session_state['points']['lost'][1]} if st.session_state['points']['lost'] else {'lat': None, 'lon': None},
86
+ {'lat': st.session_state['points']['end'][0], 'lon': st.session_state['points']['end'][1]} if st.session_state['points']['end'] else {'lat': None, 'lon': None}
87
+ ])
88
+
89
+ map_data.dropna(inplace=True)
90
+
91
+ layer = pdk.Layer(
92
+ 'ScatterplotLayer',
93
+ data=map_data,
94
+ get_position='[lon, lat]',
95
+ get_color='[200, 30, 0, 160]',
96
+ get_radius=200,
97
+ )
98
+
99
+ view_state = pdk.ViewState(
100
+ latitude=map_data['lat'].mean() if not map_data.empty else 0,
101
+ longitude=map_data['lon'].mean() if not map_data.empty else 0,
102
+ zoom=10,
103
+ pitch=0,
104
+ )
105
 
106
+ r = pdk.Deck(
107
+ map_style='mapbox://styles/mapbox/light-v9',
108
+ layers=[layer],
109
+ initial_view_state=view_state,
110
+ tooltip={"text": "{pointType} point at {lat}, {lon}"}
111
+ )
112
 
113
+ st.pydeck_chart(r)
 
 
114
 
115
  if st.button("Save"):
116
+ submit_data(age, gender, transport, multi_transport, time_of_day, day_of_week, description, st.session_state['points'])
117
 
118
  st.markdown("---")
119
  st.markdown("For a more detailed survey, click the link or scan the QR code:")
requirements.txt CHANGED
@@ -3,4 +3,6 @@
3
  # platform: osx-64
4
  streamlit
5
  psycopg2-binary
 
 
6
 
 
3
  # platform: osx-64
4
  streamlit
5
  psycopg2-binary
6
+ pandas
7
+ pydeck
8