pjgerrits commited on
Commit
a0c5560
1 Parent(s): 0669452

add query cache

Browse files
Files changed (1) hide show
  1. app.py +10 -3
app.py CHANGED
@@ -30,11 +30,14 @@ def connect_to_db():
30
  st.error(f"An error occurred while connecting to the database: {e}")
31
  return None
32
 
33
- conn = connect_to_db()
34
 
 
 
 
35
  # Function to fetch previously saved data points
36
  def fetch_saved_data():
37
- # conn = connect_to_db()
38
  if conn:
39
  cursor = conn.cursor()
40
  cursor.execute('SELECT "X", "Y", description, gl_certainty FROM public.gettinglost_tracking')
@@ -44,7 +47,10 @@ def fetch_saved_data():
44
  return data
45
  return []
46
 
 
 
47
  # Function to save data to the database
 
48
  def save_data(lat, lon, description, rating):
49
  conn = connect_to_db()
50
  if conn:
@@ -53,7 +59,7 @@ def save_data(lat, lon, description, rating):
53
  (lat, lon, lon, lat, description, rating))
54
  conn.commit()
55
  cursor.close()
56
- conn.close()
57
  st.sidebar.success("Data recorded successfully!")
58
  else:
59
  st.sidebar.error("Failed to save data.")
@@ -67,6 +73,7 @@ draw = Draw(draw_options={'polyline': False, "marker": True, 'rectangle': False,
67
  m.add_child(draw)
68
 
69
  # Display previously saved data points on the map
 
70
  for point in fetch_saved_data():
71
  lat, lon, desc, rating = point
72
  folium.Marker([lat, lon], popup=f"{desc} (Certainty: {rating})").add_to(m)
 
30
  st.error(f"An error occurred while connecting to the database: {e}")
31
  return None
32
 
33
+ # conn = connect_to_db()
34
 
35
+ # Perform query.
36
+ # Uses st.cache_data to only rerun when the query changes or after 10 min.
37
+ @st.cache_data(ttl=600)
38
  # Function to fetch previously saved data points
39
  def fetch_saved_data():
40
+ conn = connect_to_db()
41
  if conn:
42
  cursor = conn.cursor()
43
  cursor.execute('SELECT "X", "Y", description, gl_certainty FROM public.gettinglost_tracking')
 
47
  return data
48
  return []
49
 
50
+ # Perform query.
51
+ # Uses st.cache_data to only rerun when the query changes or after 10 min.
52
  # Function to save data to the database
53
+ @st.cache_data(ttl=600)
54
  def save_data(lat, lon, description, rating):
55
  conn = connect_to_db()
56
  if conn:
 
59
  (lat, lon, lon, lat, description, rating))
60
  conn.commit()
61
  cursor.close()
62
+ # conn.close()
63
  st.sidebar.success("Data recorded successfully!")
64
  else:
65
  st.sidebar.error("Failed to save data.")
 
73
  m.add_child(draw)
74
 
75
  # Display previously saved data points on the map
76
+ # conn = connect_to_db()
77
  for point in fetch_saved_data():
78
  lat, lon, desc, rating = point
79
  folium.Marker([lat, lon], popup=f"{desc} (Certainty: {rating})").add_to(m)