Skip to content

Commit 2717f8c

Browse files
committed
added markdown tutorial for couchbase_streamlit_connector
1 parent 8e490b2 commit 2717f8c

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Couchbase Connector for Streamlit
2+
3+
## 1. Introduction
4+
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.
5+
6+
For a working demo please checkout `src/Demo.py` file. You can run it by the command
7+
```bash
8+
git clone https://github.com/Couchbase-Ecosystem/couchbase_streamlit_connector.git
9+
cd ./couchbase_streamlit_connector
10+
pip install -r requirements.txt
11+
pip install plotly geopy numpy
12+
streamlit run src/Demo.py
13+
```
14+
15+
## 2. Prerequisites
16+
### System Requirements
17+
- Python 3.10 or higher installed.
18+
- Ensure that the Python version is [compatible](https://docs.couchbase.com/python-sdk/current/project-docs/compatibility.html#python-version-compat) with the Couchbase SDK.
19+
- Couchbase Capella account ([Docs](https://docs.couchbase.com/cloud/get-started/intro.html))
20+
- An operational cluster created in a project
21+
- Configured cluster access permissions and allowed IP addresses ([Docs](https://docs.couchbase.com/cloud/get-started/connect.html#prerequisites))
22+
- Connection string obtained from Couchbase Capella
23+
24+
### Installing Dependencies
25+
To install the required dependencies, run:
26+
```sh
27+
git clone https://github.com/Couchbase-Ecosystem/couchbase_streamlit_connector.git
28+
pip install ./couchbase_streamlit_connector/
29+
```
30+
31+
## 3. Usage Guide
32+
33+
### Initializing the Connector
34+
You can set up the Couchbase connection using either of the following methods:
35+
36+
#### **Option 1: Using `secrets.toml` (Recommended)**
37+
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):
38+
39+
```toml
40+
[connections.couchbase]
41+
CONNSTR = "<CONNECTION_STRING>"
42+
USERNAME = "<CLUSTER_ACCESS_USERNAME>"
43+
PASSWORD = "<CLUSTER_ACCESS_PASSWORD>"
44+
BUCKET_NAME = "<BUCKET_NAME>"
45+
SCOPE_NAME = "<SCOPE_NAME>"
46+
COLLECTION_NAME = "<COLLECTION_NAME>"
47+
```
48+
49+
Then, initialize the connection in your Streamlit application:
50+
51+
```python
52+
import streamlit as st
53+
from couchbase_streamlit_connector.connector import CouchbaseConnector
54+
55+
connection = st.connection(
56+
"couchbase",
57+
type=CouchbaseConnector
58+
)
59+
st.help(connection)
60+
```
61+
62+
#### **Option 2: Passing Credentials Directly (Alternative)**
63+
Alternatively, you can pass the connection details as keyword arguments:
64+
65+
```python
66+
import streamlit as st
67+
from couchbase_streamlit_connector.connector import CouchbaseConnector
68+
69+
connection = st.connection(
70+
"couchbase",
71+
type=CouchbaseConnector,
72+
CONNSTR="<CONNECTION_STRING>",
73+
USERNAME="<USERNAME>",
74+
PASSWORD="<PASSWORD>",
75+
BUCKET_NAME="<BUCKET_NAME>",
76+
SCOPE_NAME="<SCOPE_NAME>",
77+
COLLECTION_NAME="<COLLECTION_NAME>"
78+
)
79+
st.help(connection)
80+
```
81+
82+
### Performing CRUD Operations
83+
84+
#### **Insert a Document**
85+
```python
86+
connection.insert_document("222", {"key": "value"})
87+
st.write(connection.get_document("222"))
88+
```
89+
90+
#### **Fetch a Document**
91+
```python
92+
st.write(connection.get_document("111"))
93+
```
94+
95+
#### **Replace a Document**
96+
```python
97+
connection.replace_document("222", {"new_key": "new_value"})
98+
st.write(connection.get_document("222"))
99+
```
100+
101+
#### **Delete a Document**
102+
```python
103+
connection.remove_document("222")
104+
st.write("Document 222 deleted")
105+
```
106+
107+
#### **Run a Query**
108+
```python
109+
result = connection.query("SELECT * FROM `travel-sample`.`inventory`.`airline` LIMIT 5;")
110+
st.write(result)
111+
```

0 commit comments

Comments
 (0)