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
@@ -296,16 +296,171 @@ Now we'll build a simple web UI to call AppSync and visualize hotels on a map.
296
296
**What is Streamlit?**
297
297
Streamlit is a Python framework for building data apps with minimal code. You write Python functions, and Streamlit renders them as interactive web UIs.
298
298
299
-
**Steps:**
299
+
**Application structure:**
300
+
We'll build a multi-page Streamlit app with two Python files:
301
+
1.**`home.py`** — Main entry point with navigation, connection settings, and a home page
302
+
2.**`search_hotels.py`** — Hotel search page with city filter and interactive map visualization
303
+
304
+
#### Setup
305
+
306
+
Create and activate a virtual environment, then install dependencies:
307
+
```bash
308
+
python3 -m venv .venv
309
+
source .venv/bin/activate
310
+
pip install streamlit requests pandas pydeck
311
+
```
300
312
301
-
1. Create and activate a virtual environment, then install dependencies:
302
-
```bash
303
-
python3 -m venv .venv
304
-
source .venv/bin/activate
305
-
pip install streamlit requests pandas pydeck
306
-
```
313
+
#### Build the navigation and connection settings (`home.py`)
307
314
308
-
2. Create a file (e.g., `search_hotels.py`) with the following code.
315
+
**What does `home.py` do?**
316
+
This is the main entry point for the Streamlit app. It provides:
317
+
- A home page with information about the demo
318
+
- A sidebar navigation menu to switch between pages
319
+
- Connection settings inputs (GraphQL endpoint, API key, Couchbase credentials) stored in Streamlit's `session_state`
320
+
- Dynamic page loading based on user selection
321
+
- Validation to ensure required settings are filled before accessing feature pages
322
+
323
+
**Code walkthrough:**
324
+
325
+
```python
326
+
import importlib
327
+
import streamlit as st
328
+
329
+
330
+
PAGES= {
331
+
"Home": "home",
332
+
"Search Hotels": "search_hotels",
333
+
}
334
+
335
+
336
+
defrender_home():
337
+
"""
338
+
Display the home page with information about the demo.
339
+
Explains what the app does and how AppSync + Data API + Streamlit work together.
340
+
"""
341
+
st.title("Home")
342
+
st.subheader("About this demo")
343
+
st.markdown(
344
+
"This Streamlit app calls an AWS AppSync GraphQL API that uses Couchbase Data API behind the scenes to search hotels by city and visualize results on a map."
345
+
)
346
+
st.markdown(
347
+
"**Why dataAPI for serverless?** It keeps credentials and query logic secure on the server behind AppSync, avoids heavy SDK initialization overhead, and perfectly fits stateless, scalable Lambda functions."
348
+
)
349
+
st.subheader("What this demo showcases and how to proceed")
350
+
st.markdown(
351
+
"- Enter your AppSync GraphQL endpoint and API key in the sidebar (plus Couchbase creds).\n"
352
+
"- Go to 'Search Hotels' to run a city filter; resolvers invoke dataAPI to query Couchbase.\n"
353
+
"- View results in a list and on a map; try different cities.\n"
354
+
"- Extend this starter by adding mutations or subscriptions in your AppSync schema."
355
+
)
356
+
357
+
358
+
defrender():
359
+
"""
360
+
Main render function: sets up sidebar navigation and connection settings.
361
+
Flow:
362
+
1. Display navigation menu in sidebar (Home, Search Hotels, etc.)
363
+
2. Collect connection settings (GraphQL endpoint, API key, Couchbase username/password)
364
+
- These are stored in session_state so they persist across page changes
365
+
3. If user is on the Home page, render it directly
366
+
4. For other pages, validate that all required connection settings are filled
367
+
5. Dynamically import and render the selected page module
368
+
369
+
Why session_state?
370
+
Streamlit reruns the entire script on every interaction. session_state persists data
371
+
across reruns, so users don't have to re-enter connection settings when switching pages.
372
+
373
+
Why dynamic imports?
374
+
Instead of hardcoding imports for all pages, we use importlib to load page modules
375
+
based on the user's selection. This keeps the code modular and extensible.
0 commit comments