|
| 1 | +--- |
| 2 | +# frontmatter |
| 3 | +path: "/tutorial-couchbase-streamlit-connector" |
| 4 | +title: Couchbase Connector for Streamlit |
| 5 | +short_title: Couchbase Connector for Streamlit |
| 6 | +description: |
| 7 | + - Learn how to integrate streamlit with Couchbase Capella |
| 8 | + - Example on CRUD and query operations |
| 9 | +content_type: tutorial |
| 10 | +filter: sdk |
| 11 | +technology: |
| 12 | + - capella |
| 13 | + - query |
| 14 | +tags: |
| 15 | + - Streamlit |
| 16 | +sdk_language: |
| 17 | + - python |
| 18 | +length: 30 Mins |
| 19 | +--- |
| 20 | + |
| 21 | + |
| 22 | +# [Couchbase Connector for Streamlit](https://couchbase-st-tutorial.streamlit.app/) |
| 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 |
| 28 | +```bash |
| 29 | +git clone https://github.com/Couchbase-Ecosystem/couchbase_streamlit_connector.git |
| 30 | +cd ./couchbase_streamlit_connector |
| 31 | +pip install -r requirements.txt |
| 32 | +pip install plotly geopy numpy |
| 33 | +streamlit run src/Demo.py |
| 34 | +``` |
| 35 | +Or you can jave a look at it through this link [Demo App](https://couchbase-connector-demo-app.streamlit.app/) |
| 36 | + |
| 37 | +## 2. Prerequisites |
| 38 | +### 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 | +``` |
| 48 | + |
| 49 | +## 3. Usage Guide |
| 50 | + |
| 51 | +### Initializing the Connector |
| 52 | +You can set up the Couchbase connection using either of the following methods: |
| 53 | + |
| 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 | + |
| 57 | +```toml |
| 58 | +[connections.couchbase] |
| 59 | +CONNSTR = "<CONNECTION_STRING>" |
| 60 | +USERNAME = "<CLUSTER_ACCESS_USERNAME>" |
| 61 | +PASSWORD = "<CLUSTER_ACCESS_PASSWORD>" |
| 62 | +BUCKET_NAME = "<BUCKET_NAME>" |
| 63 | +SCOPE_NAME = "<SCOPE_NAME>" |
| 64 | +COLLECTION_NAME = "<COLLECTION_NAME>" |
| 65 | +``` |
| 66 | + |
| 67 | +Then, initialize the connection in your Streamlit application: |
| 68 | + |
| 69 | +```python |
| 70 | +import streamlit as st |
| 71 | +from couchbase_streamlit_connector.connector import CouchbaseConnector |
| 72 | + |
| 73 | +connection = st.connection( |
| 74 | + "couchbase", |
| 75 | + type=CouchbaseConnector |
| 76 | +) |
| 77 | +st.help(connection) |
| 78 | +``` |
| 79 | + |
| 80 | +#### **Option 2: Passing Credentials Directly (Alternative)** |
| 81 | +Alternatively, you can pass the connection details as keyword arguments: |
| 82 | + |
| 83 | +```python |
| 84 | +import streamlit as st |
| 85 | +from couchbase_streamlit_connector.connector import CouchbaseConnector |
| 86 | + |
| 87 | +connection = st.connection( |
| 88 | + "couchbase", |
| 89 | + type=CouchbaseConnector, |
| 90 | + CONNSTR="<CONNECTION_STRING>", |
| 91 | + USERNAME="<USERNAME>", |
| 92 | + PASSWORD="<PASSWORD>", |
| 93 | + BUCKET_NAME="<BUCKET_NAME>", |
| 94 | + SCOPE_NAME="<SCOPE_NAME>", |
| 95 | + COLLECTION_NAME="<COLLECTION_NAME>" |
| 96 | +) |
| 97 | +st.help(connection) |
| 98 | +``` |
| 99 | + |
| 100 | +### Performing CRUD Operations |
| 101 | + |
| 102 | +#### **Insert a Document** |
| 103 | +```python |
| 104 | +connection.insert_document("222", {"key": "value"}) |
| 105 | +st.write(connection.get_document("222")) |
| 106 | +``` |
| 107 | + |
| 108 | +#### **Fetch a Document** |
| 109 | +```python |
| 110 | +st.write(connection.get_document("111")) |
| 111 | +``` |
| 112 | + |
| 113 | +#### **Replace a Document** |
| 114 | +```python |
| 115 | +connection.replace_document("222", {"new_key": "new_value"}) |
| 116 | +st.write(connection.get_document("222")) |
| 117 | +``` |
| 118 | + |
| 119 | +#### **Delete a Document** |
| 120 | +```python |
| 121 | +connection.remove_document("222") |
| 122 | +st.write("Document 222 deleted") |
| 123 | +``` |
| 124 | + |
| 125 | +#### **Run a Query** |
| 126 | +```python |
| 127 | +result = connection.query("SELECT * FROM `travel-sample`.`inventory`.`airline` LIMIT 5;") |
| 128 | +st.write(result) |
| 129 | +``` |
| 130 | + |
| 131 | +## 4. Appendix |
| 132 | + |
| 133 | +Here are some helpful resources for working with Couchbase and Streamlit: |
| 134 | + |
| 135 | +### **Couchbase Documentation** |
| 136 | +- [Couchbase Python SDK Compatibility](https://docs.couchbase.com/python-sdk/current/project-docs/compatibility.html#python-version-compat) |
| 137 | +- [Getting Started with Couchbase Capella](https://docs.couchbase.com/cloud/get-started/intro.html) |
| 138 | +- [Connecting to Couchbase Capella](https://docs.couchbase.com/cloud/get-started/connect.html#prerequisites) |
| 139 | +- [N1QL Query Language Guide](https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/index.html) |
| 140 | +- [Couchbase SDKs Overview](https://docs.couchbase.com/home/sdk.html) |
| 141 | + |
| 142 | +### **Streamlit Documentation** |
| 143 | +- [Streamlit Secrets Management](https://docs.streamlit.io/develop/concepts/connections/secrets-management) |
| 144 | +- [Using `st.connection`](https://docs.streamlit.io/develop/api-reference/connections) |
| 145 | +- [Streamlit Components](https://docs.streamlit.io/develop/api-reference) |
| 146 | + |
| 147 | +### **Additional Resources** |
| 148 | +- [Couchbase Sample Data](https://docs.couchbase.com/server/current/tools/cbimport-json.html) |
| 149 | +- [Demo App](https://couchbase-connector-demo-app.streamlit.app/) |
0 commit comments