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
# [Couchbase Connector for Streamlit](https://couchbase-st-tutorial.streamlit.app/)
23
23
24
-
## 1. Introduction
25
-
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.
26
-
27
-
For a working demo please checkout `src/Demo.py` file. You can run it by the command
Or you can jave a look at it through this link [Demo App](https://couchbase-connector-demo-app.streamlit.app/)
24
+
## Introduction
25
+
This comprehensive tutorial repository guides developers through integrating Couchbase with Streamlit applications. Unlike a simple demo app, this repository focuses on teaching the fundamentals, best practices, and interactive implementation of Couchbase within Streamlit applications.
26
+
27
+
## Table of Contents
28
+
1.[Goals](#goals)
29
+
2.[Prerequisites](#prerequisites)
30
+
3.[Installation](#installation)
31
+
4.[Core Concepts](#core-concepts)
32
+
5.[Tutorial Sections](#tutorial-sections)
33
+
6.[Running Your Application](#running-your-application)
34
+
7.[Conclusion](#conclusion)
35
+
8.[Appendix](#appendix)
36
+
37
+
## Goals
38
+
- Master Couchbase integration with Streamlit applications
39
+
- Understand core Couchbase concepts and their application in Streamlit
40
+
- Learn through hands-on, working example
41
+
42
+
## Prerequisites
36
43
37
-
## 2. Prerequisites
38
44
### System Requirements
39
-
- Ensure you have **Python 3.10 or higher** (check [compatibility](https://docs.couchbase.com/python-sdk/current/project-docs/compatibility.html#python-version-compat) with the Couchbase SDK), a **Couchbase Capella account** ([Docs](https://docs.couchbase.com/cloud/get-started/intro.html)), and an **operational cluster** created in a project.
40
-
- Configured cluster access permissions and allowed IP addresses ([Docs](https://docs.couchbase.com/cloud/get-started/connect.html#prerequisites))
41
-
- Connection string obtained from Couchbase Capella
42
-
43
-
### Installing Dependencies
44
-
To install the required dependencies, run:
45
-
```sh
46
-
pip install couchbase-streamlit-connector
47
-
```
45
+
- Python 3.10 or higher ([Compatibility Guide](https://docs.couchbase.com/python-sdk/current/project-docs/compatibility.html#python-version-compat))
- Active Couchbase cluster with connection credentials
48
48
49
-
## 3. Usage Guide
49
+
### Required Knowledge
50
+
- Basic Python programming
51
+
- Fundamental understanding of web applications
52
+
- Basic database concepts
50
53
51
-
### Initializing the Connector
52
-
You can set up the Couchbase connection using either of the following methods:
54
+
## Installation
53
55
54
-
#### **Option 1: Using `secrets.toml` (Recommended)**
55
-
For better security and convenience, store your credentials in a `.streamlit/secrets.toml` file at the root of your project. Learn more about [Streamlit Secrets Management](https://docs.streamlit.io/develop/concepts/connections/secrets-management):
56
+
1.**Set up your Python environment**:
57
+
```bash
58
+
python -m venv venv
59
+
source venv/bin/activate # On Windows: venv\Scripts\activate
Alternatively, you can pass the connection details as keyword arguments:
91
+
#### Why Couchbase Uses JSON
92
+
-**Flexible Schema**: Adapt to changing data requirements
93
+
-**Efficient Querying**: Native support for SQL-like queries (N1QL)
94
+
-**Scalability**: Easy to distribute and replicate
95
+
-**Natural Data Representation**: Matches application objects
96
+
97
+
### Couchbase Architecture Overview
98
+
-**Buckets**: Top-level containers for data
99
+
-**Scopes**: Namespaces within buckets
100
+
-**Collections**: Groups of related documents
101
+
-**Documents**: Individual JSON data records
102
+
103
+
### Important Operation Notes
104
+
-**CRUD Operations**: Create, Read, Update, and Delete operations only work on the specific bucket, scope, and collection specified during connection setup.
105
+
-**Queries**: Can work across any bucket, scope, and collection in the cluster, regardless of the connection settings.
106
+
-**Access Control**: Both CRUD operations and queries are limited by the permissions assigned to the Couchbase user in the cluster.
107
+
108
+
## Tutorial Sections
82
109
110
+
### 1. Setting Up Your First Application
111
+
112
+
Create a new file `app.py`:
83
113
```python
84
114
import streamlit as st
115
+
import json
85
116
from couchbase_streamlit_connector.connector import CouchbaseConnector
This function creates new documents in Couchbase by accepting a document ID and JSON data. It uses json.loads() instead of eval() for secure JSON parsing, protecting against code injection. The function displays success or error messages based on the operation outcome.
This function retrieves documents from Couchbase using their document IDs. It displays the document contents in a formatted JSON viewer if found, or shows an error message if the document doesn't exist or there's a connection issue.
107
187
108
-
#### **Fetch a Document**
188
+
#### Update Operation
109
189
```python
110
-
st.write(connection.get_document("111"))
190
+
defupdate_document():
191
+
st.subheader("Update Document")
192
+
with st.expander("Update an existing document", expanded=False):
193
+
doc_id = st.text_input("Document ID to update", key="update_id")
This function updates existing documents by replacing their entire content with new JSON data. It requires both the document ID and the complete new document content, ensuring data consistency by using json.loads() for safe JSON parsing.
This function removes documents from the database using their document IDs. It provides immediate feedback through success/error messages and handles cases where the document might not exist.
118
223
119
-
#### **Delete a Document**
224
+
###3. Querying Data
120
225
```python
121
-
connection.remove_document("222")
122
-
st.write("Document 222 deleted")
226
+
defquery_data():
227
+
st.subheader("Query Data")
228
+
with st.expander("Execute SQL++ Query", expanded=False):
229
+
query = st.text_area(
230
+
"SQL++ Query",
231
+
value="SELECT * FROM `travel-sample`.inventory.airline LIMIT 5;",
This function executes SQL++ (N1QL) queries against Couchbase. The for row in results loop is necessary because Couchbase query results are returned as an iterator to efficiently handle large result sets. Converting the iterator to a list allows Streamlit to display all results at once while managing memory usage effectively.
124
245
125
-
#### **Run a Query**
246
+
###Main function
126
247
```python
127
-
result = connection.query("SELECT * FROM `travel-sample`.`inventory`.`airline` LIMIT 5;")
128
-
st.write(result)
248
+
defmain():
249
+
# Initialize connection in sidebar
250
+
initialize_connection()
251
+
252
+
# Main content area
253
+
if"connection"in st.session_state:
254
+
# Add tabs for different operations
255
+
tab1, tab2, tab3, tab4, tab5 = st.tabs([
256
+
"Create", "Read", "Update", "Delete", "Query"
257
+
])
258
+
259
+
with tab1:
260
+
insert_document()
261
+
with tab2:
262
+
fetch_document()
263
+
with tab3:
264
+
update_document()
265
+
with tab4:
266
+
delete_document()
267
+
with tab5:
268
+
query_data()
269
+
else:
270
+
st.info("Please connect to Couchbase using the sidebar to start.")
271
+
272
+
if__name__=="__main__":
273
+
st.set_page_config(
274
+
page_title="Couchbase Streamlit Demo",
275
+
page_icon="🔌",
276
+
layout="wide"
277
+
)
278
+
main()
129
279
```
130
280
131
-
## 4. Appendix
281
+
## Running Your Application
282
+
283
+
1.**Start the Streamlit application**:
284
+
```bash
285
+
streamlit run app.py
286
+
```
287
+
288
+
2.**Access the application**:
289
+
- Open your browser to `http://localhost:8501`
290
+
- Enter your Couchbase connection details
291
+
- Start interacting with your data
292
+
293
+
### Verifying Changes in Couchbase Capella
294
+
295
+
After performing CRUD operations or queries, you can verify the changes in Couchbase Capella:
- Use the same query you executed in your Streamlit app
306
+
- Modify the query to explore related data
307
+
5. You can also use the Documents browser in Capella to directly view and edit documents
308
+
309
+
## Conclusion
310
+
This repository serves as an educational resource for developers who want to integrate Couchbase into Streamlit applications. By following these tutorials, users can learn how to query, display, and optimize Couchbase data in Streamlit apps.
311
+
312
+
## Appendix
132
313
133
314
Here are some helpful resources for working with Couchbase and Streamlit:
0 commit comments