This project provides a seamless integration between Streamlit and Couchbase, allowing developers to interact with Couchbase databases effortlessly. It enables users to fetch, insert, update, and delete data within Streamlit applications without needing to switch between different SDKs, enhancing the overall development experience.
- Python 3.10 or higher (Check compatibility)
- Couchbase Capella account (Setup Guide)
- Operational Couchbase cluster with configured access (Instructions)
- Connection string from Couchbase Capella
To install the required dependencies, run:
pip install couchbase streamlit couchbase-streamlit-connector
- Clone the repository and install dependencies.
- Configure the connection details as described above.
- Run the demo app using:
git clone https://github.com/Couchbase-Ecosystem/couchbase_streamlit_connector.git
cd ./couchbase_streamlit_connector
pip install -r requirements.txt
pip install plotly geopy numpy
streamlit run src/Demo.py
Or access the hosted version: Demo App
- Fork the repository.
- Refer to the Streamlit documentation for guidance on deploying your app: Deploy your app.
- Navigate to Streamlit Community Cloud.
- Deploy the repository by selecting it and adding the required secrets.
This Streamlit application demonstrates how to connect to a Couchbase database and visualize data using interactive maps and charts. It utilizes the travel-sample
dataset, focusing on airports, routes, landmarks, and hotels.
Connection:
- Allows users to input Couchbase connection credentials via a sidebar.
- Establishes a connection using
st.connection
andCouchbaseConnector
. - Code Snippet:
if st.sidebar.button("Connect"):
try:
connection = st.connection(
"couchbase",
type=CouchbaseConnector,
CONNSTR=conn_str,
USERNAME=username,
PASSWORD=password,
BUCKET_NAME=bucket_name,
SCOPE_NAME=scope_name,
COLLECTION_NAME=collection_name
)
st.session_state["connection"] = connection
st.sidebar.success("Connected successfully!")
except Exception as e:
st.sidebar.error(f"Connection failed: {e}")
Tab 1: Flight Routes Map:
- Fetches airport and route data from Couchbase using N1QL queries.
- Displays airports as markers on a map and flight routes as lines.
- Code Snippet:
@st.cache_data
def get_all_airports(_connection):
query = """
SELECT geo.lat, geo.lon, city, country, airportname as name, faa, icao, id
FROM `travel-sample`.inventory.airport
WHERE geo.lat IS NOT NULL
AND geo.lon IS NOT NULL
AND faa IS NOT NULL;
"""
result = _connection.query(query)
return pd.DataFrame([row for row in result.rows()])
Tab 2: Find Hotels Near Landmarks:
- Retrieves landmark and hotel data.
- Calculates distances using
geopy.distance
. - Displays landmarks and nearby hotels on a map.
- Code Snippet:
def get_hotels_near_landmark(_connection, landmark_lat, landmark_lon, max_distance_km=10):
# ...query hotel data...
for row in result:
hotel_coords = (row['lat'], row['lon'])
landmark_coords = (landmark_lat, landmark_lon)
distance = geodesic(hotel_coords, landmark_coords).kilometers
# ...filter hotels by distance...
Tab 3: Find Hotels in Cities:
- Fetches hotel and city data.
- Displays hotels on a map, color-coded by average rating.
- Code Snippet:
def create_hotel_map(hotels_df):
# ...create plotly scatter map...
fig = px.scatter_map(
rated_hotels,
lat="lat",
lon="lon",
color="avg_rating",
# ...other parameters...
)
# ...add non rated hotels...
st.plotly_chart(fig, use_container_width=True)
- Connection: Users input Couchbase credentials and connect.
- Data Retrieval: The application executes N1QL queries.
- Data Processing: Data is transformed into Pandas DataFrames.
- Visualization: Interactive maps and charts are generated using Plotly.
- User Interaction: Users can select airports, landmarks, and cities to filter the data and update the visualizations.
- Couchbase Documentation: docs.couchbase.com
- Comprehensive documentation for Couchbase products and services.
- Couchbase Python SDK Documentation: docs.couchbase.com/python-sdk/current/
- Detailed information on using the Couchbase Python SDK.
- Streamlit Documentation: docs.streamlit.io
- Official documentation for Streamlit, including API references and tutorials.
- Streamlit Community Cloud: share.streamlit.io
- Platform for deploying and sharing Streamlit applications.
- Geopy Documentation: geopy.readthedocs.io
- Python Geocoding Toolbox.
- Plotly Documentation: plotly.com/python/
- Python graphing library.