You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorial/markdown/python/streamlit/flight_search_streamlit_couchbase.md
+30-31Lines changed: 30 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -155,8 +155,8 @@ streamlit hello
155
155
156
156
If everything is set up correctly, a browser window should open with Streamlit's demo application.
157
157
158
-
### Implement Data Fetching Functions
159
-
To optimize performance, data retrieval functions are cached using `@st.cache_data`, which stores previously fetched data to prevent redundant queries and speed up the app. However, the `_connection`object is intentionally not cached (indicated by the underscore prefix) to ensure a fresh database connection is established each time. Caching the connection could lead to issues with stale or expired sessions, potentially causing failed queries or inconsistent data retrieval. For more details, refer to the official documentation: [Streamlit `st.cache_data`](https://docs.streamlit.io/develop/api-reference/caching-and-state/st.cache_data).
158
+
### Implement Data Fetching Functions
159
+
To optimize performance, data retrieval functions are cached using `@st.cache_data`, which stores previously fetched data to prevent redundant queries and speed up the app. However, the `_connection`argument is not cached because database connection objects are not hashable. The underscore prefix is used to explicitly exclude it from caching, ensuring that Streamlit does not attempt to hash the connection. Since `@st.cache_data` requires function arguments to be hashable, unhashable objects like database connections must be excluded to avoid errors. For more details, refer to the official documentation: [Streamlit `st.cache_data`](https://docs.streamlit.io/develop/api-reference/caching-and-state/st.cache_data).
airports_faa ="["# Initialize a string to store FAA codes in a list format
184
-
for i inrange(len(selected_airports_df)):
185
-
if i !=len(selected_airports_df) -1:
186
-
airports_faa +=f'"{(selected_airports_df.iloc[i])["faa"]}", '# Append each FAA code with a comma
187
-
else:
188
-
airports_faa +=f'"{(selected_airports_df.iloc[i])["faa"]}"'# Append last FAA code without a comma
189
-
airports_faa +="]"
183
+
airports_faa =str(selected_airports_df["faa"].to_list()) # Initialize a string to store FAA codes in a list format
190
184
query =f"""
191
185
SELECT * FROM `travel-sample`.`inventory`.`route`
192
186
WHERE (sourceairport IN {airports_faa} AND destinationairport IN {airports_faa});
@@ -296,13 +290,7 @@ This function retrieves hotel data from the `travel-sample.inventory.hotel` coll
296
290
```python
297
291
@st.cache_data
298
292
defget_all_hotels(_connection, cities):
299
-
cities_str ="["# Initialize the string for city names
300
-
for i inrange(len(cities)):
301
-
if i !=len(cities) -1:
302
-
cities_str +=f'"{cities[i]}", '# Add city name with a comma
303
-
else:
304
-
cities_str +=f'"{cities[i]}"'# Add last city without a comma
305
-
cities_str +="]"
293
+
cities_str =f"{cities}"
306
294
query =f"""
307
295
SELECT h.*, geo.lat as lat, geo.lon as lon, ARRAY_AVG(ARRAY r.ratings.Overall FOR r IN h.reviews WHEN r.ratings.Overall IS NOT MISSING END) as avg_rating
308
296
FROM `travel-sample`.inventory.hotel h
@@ -328,11 +316,8 @@ This function visualizes flight routes between airports using [Plotly](https://p
0 commit comments