st-xatadb-connection


Namest-xatadb-connection JSON
Version 1.0.3 PyPI version JSON
download
home_pagehttps://github.com/SergioSKA27/st-xatadb-connection
SummaryStreamlit Xata Data Base ConnectionAn easy way to connect your Streamlit application to your Xata database.
upload_time2024-01-04 11:00:53
maintainer
docs_urlNone
authorSergio Demis Lopez Martinez
requires_python
licenseMIT
keywords streamlit xata connection integration database
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # st-xatadb-connection

## Xata Data Base Connection Streamlit

In the realm of web development, the combination of Xata and Streamlit is nothing short of a dream team.
Xata,with its schemaless relational SQL database and simple REST API, provides a flexible and scalable foundation for storing and managing data.
 Streamlit, on the other hand, is an open-source framework that empowers developers to create
interactive web applications with minimal effort. Together, they form a synergistic partnership that unlocks a world of possibilities for building sophisticated web applications.

---

To further enhance the synergy between Xata and Streamlit, I've created the st_xatadb_connection package.
This package provides a seamless bridge between the two tools, making it incredibly easy to connect your Streamlit application to your Xata database.With just a few lines of code, you can perform CRUD operations (create, retrieve, update, delete) on your Xata data, generate reports and visualizations, and even interact with your data in real time.
The st_xatadb_connection package takes care of all the heavy lifting, allowing you to focus on building your application's functionality.

## Getting Started with st_xata_connection

## 1. Set up your Xata.io and Streamlit Environment

- Create a Xata account and database at [Xata Web](https://xata.io).

- Generate an API key for your Xata database.

- Get your Xata database URL endpoint.

- Install Streamlit by running `pip install streamlit`.

- Install st_xata_connection by running `pip install st-xatadb-connection`.

## 2. Configure your Xata Credentials

To securely store your Xata API key and database URL, you can use Streamlit's secrets manager or environment variables.

``` toml
XATA_API_KEY = "YOUR_XATA_API_KEY"

DATABASE_URL = "YOUR_XATA_DATABASE_URL"
```

## 3. Connect to your Xata.io Database

Import the `st_xata_connection` package and use the st.connection() function to connect to your Xata database.
You could also use the the `st.session_state` object to store the connection object and reuse it across your Streamlit app.

``` python
import streamlit as st
from st_xatadb_connection import XataConnection

xata = st.connection('xata', type=XataConnection)
```

## 4. Query your Xata.io Database

Use the `xata.query()` function to query your Xata.io database.

``` python
results = xata.query("Table_Name")
```

## 5. Display your Query Results

Use Streamlit functions like `st.write()`, `st.table()`,`st.json()`, or `st.dataframe()` to display your query results in your Streamlit app.

```python
st.write(results)
```

## 6. Insert, Update, and Delete Data

You can also use st_xatadb_connection to insert, update, and delete data in your Xata.io database.

``` python
record = {
    "name": "John Doe",
    "age": 30,
    "email": "a@b.com"
}
response = xata.insert("Table_Name", record)
```

The `xata.insert()` function returns a response object that contains the information about the inserted record.
It looks like this:

```json
{
      "id": "rec_c8hnbch26un1nl0rthkg",
      "xata": {
        "version": 0,
        "createdAt": "2023-05-15T08:21:31.96526+01:00",
        "updatedAt": "2023-05-15T21:58:54.072595+01:00"
      }
}
```

where `id` is the unique identifier of the inserted record, and `xata` contains the metadata about the record.
Now we can use the `id` to update the record or delete it. And in the same way, these functions also return a response object
that contains the information about the updated or deleted record.

```python
update_response = xata.update("Table_Name", "rec_c8hnbch26un1nl0rthkg", {"age": 31})
delete_response = xata.delete("Table_Name", "rec_c8hnbch26un1nl0rthkg")
```

## 7. Working with files

Xata supports file uploads and downloads. You can use the `xata.upload_file()` function to upload a file to your Xata database
or the `xata.get_file()` function to download a file from your Xata database.

Suppose you have a file called `my_avatar.png` in your current working directory. You can upload it to your Xata database like this:

```python
upload_response = xata.upload_file("Table_Name", "rec_c8hnbch26un1nl0rthkg", "column_name", "my_avatar_bas64_encoded")
```

Now you can download the file from your Xata database like this:

```python
download_response = xata.get_file("Table_Name", "rec_c8hnbch26un1nl0rthkg", "column_name")
```

## 8. Transactions

If you want to perform multiple operations on your Xata database as a single unit of work, you can use transactions.
For this, you can use the `xata.transaction()` function. It takes a list of operations as an argument and returns a response object.

Suppose you want to insert a record and update another record in your Xata database as a single unit of work.
You can do it like this:

```python
transaction_response = xata.transaction([
{"insert": {"table": "Table_Name", "record": {"name": "Marie Doe", "age": 21, "email": "marie@mail.com"}}}
{"update": {"table": "Table_Name", "id": "rec_c8hnbch26un1nl0rthkg", "fields": {"age": 31}}}
{"get": {"table": "Table_Name", "id": "rec_c8hnbch26un1nl0rthkg","columns": ["name", "age"]}}
{"delete": {"table": "Table_Name", "id": "rec_c8hnbch26un1nl0rthkg"}}
])
```

Note that the `transaction()` function takes a list of operations as an argument. Each operation is a dictionary that contains
the type of operation and the operation-specific arguments. The supported operations are `insert`, `update`, `get`, and `delete`.

## 9. SQL Queries

If you feel more comfortable with SQL, you can use the `xata.sql_query()` function to query your Xata database using SQL.

The `xata.sql_query()` function takes a SQL query as an argument and returns a response object with the query results.
For example, suppose you want to query your Xata database using SQL. You can do it like this:

```python
sql_response = xata.sql_query("SELECT * FROM Table_Name")
```

## 10. Asking to the AI Assistant

Xata has an AI assistant that can help you with your queries. You can use the `xata.askai()` function to ask a question to the AI assistant,
taking a reference to the table you want to query and the question you want to ask as arguments.

Suppose you want to ask the AI assistant to find the people in your table whose age is greater than 30. You can do it like this:

```python
response = xata.askai("Table_Name", "Find the people whose age is greater than 30")
```

The response looks like this:

```json
{
  "answer": "< answer >",
  "sessionId": "cg52bk1eqh5rd5hndhq95jercs",
  "records": [
    "b70d541d114ff54ad15915636450663f",
    "8ae4837002e21f013aa85c30a126ea1c",
    "4b137344a3c53d5152c45ed514188cd2"
  ]
}
```

---

The `st_xata_connection` package is a powerful tool that makes it easy to connect your Streamlit app to your Xata database.
In this article, we've covered the basics of using `st_xata_connection` to query your Xata database, insert, update, and delete data,
work with files, perform transactions, and ask questions to the AI assistant. But there's a lot more you can do with `st_xata_connection`.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/SergioSKA27/st-xatadb-connection",
    "name": "st-xatadb-connection",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "streamlit,xata,connection,integration,database",
    "author": "Sergio Demis Lopez Martinez",
    "author_email": "sergioska81@hotmail.com",
    "download_url": "https://files.pythonhosted.org/packages/02/6f/07a142f69dc7bff5491e019f54fb942f3e6cd9021fdd049d91679441310d/st-xatadb-connection-1.0.3.tar.gz",
    "platform": null,
    "description": "# st-xatadb-connection\n\n## Xata Data Base Connection Streamlit\n\nIn the realm of web development, the combination of Xata and Streamlit is nothing short of a dream team.\nXata,with its schemaless relational SQL database and simple REST API, provides a flexible and scalable foundation for storing and managing data.\n Streamlit, on the other hand, is an open-source framework that empowers developers to create\ninteractive web applications with minimal effort. Together, they form a synergistic partnership that unlocks a world of possibilities for building sophisticated web applications.\n\n---\n\nTo further enhance the synergy between Xata and Streamlit, I've created the st_xatadb_connection package.\nThis package provides a seamless bridge between the two tools, making it incredibly easy to connect your Streamlit application to your Xata database.With just a few lines of code, you can perform CRUD operations (create, retrieve, update, delete) on your Xata data, generate reports and visualizations, and even interact with your data in real time.\nThe st_xatadb_connection package takes care of all the heavy lifting, allowing you to focus on building your application's functionality.\n\n## Getting Started with st_xata_connection\n\n## 1. Set up your Xata.io and Streamlit Environment\n\n- Create a Xata account and database at [Xata Web](https://xata.io).\n\n- Generate an API key for your Xata database.\n\n- Get your Xata database URL endpoint.\n\n- Install Streamlit by running `pip install streamlit`.\n\n- Install st_xata_connection by running `pip install st-xatadb-connection`.\n\n## 2. Configure your Xata Credentials\n\nTo securely store your Xata API key and database URL, you can use Streamlit's secrets manager or environment variables.\n\n``` toml\nXATA_API_KEY = \"YOUR_XATA_API_KEY\"\n\nDATABASE_URL = \"YOUR_XATA_DATABASE_URL\"\n```\n\n## 3. Connect to your Xata.io Database\n\nImport the `st_xata_connection` package and use the st.connection() function to connect to your Xata database.\nYou could also use the the `st.session_state` object to store the connection object and reuse it across your Streamlit app.\n\n``` python\nimport streamlit as st\nfrom st_xatadb_connection import XataConnection\n\nxata = st.connection('xata', type=XataConnection)\n```\n\n## 4. Query your Xata.io Database\n\nUse the `xata.query()` function to query your Xata.io database.\n\n``` python\nresults = xata.query(\"Table_Name\")\n```\n\n## 5. Display your Query Results\n\nUse Streamlit functions like `st.write()`, `st.table()`,`st.json()`, or `st.dataframe()` to display your query results in your Streamlit app.\n\n```python\nst.write(results)\n```\n\n## 6. Insert, Update, and Delete Data\n\nYou can also use st_xatadb_connection to insert, update, and delete data in your Xata.io database.\n\n``` python\nrecord = {\n    \"name\": \"John Doe\",\n    \"age\": 30,\n    \"email\": \"a@b.com\"\n}\nresponse = xata.insert(\"Table_Name\", record)\n```\n\nThe `xata.insert()` function returns a response object that contains the information about the inserted record.\nIt looks like this:\n\n```json\n{\n      \"id\": \"rec_c8hnbch26un1nl0rthkg\",\n      \"xata\": {\n        \"version\": 0,\n        \"createdAt\": \"2023-05-15T08:21:31.96526+01:00\",\n        \"updatedAt\": \"2023-05-15T21:58:54.072595+01:00\"\n      }\n}\n```\n\nwhere `id` is the unique identifier of the inserted record, and `xata` contains the metadata about the record.\nNow we can use the `id` to update the record or delete it. And in the same way, these functions also return a response object\nthat contains the information about the updated or deleted record.\n\n```python\nupdate_response = xata.update(\"Table_Name\", \"rec_c8hnbch26un1nl0rthkg\", {\"age\": 31})\ndelete_response = xata.delete(\"Table_Name\", \"rec_c8hnbch26un1nl0rthkg\")\n```\n\n## 7. Working with files\n\nXata supports file uploads and downloads. You can use the `xata.upload_file()` function to upload a file to your Xata database\nor the `xata.get_file()` function to download a file from your Xata database.\n\nSuppose you have a file called `my_avatar.png` in your current working directory. You can upload it to your Xata database like this:\n\n```python\nupload_response = xata.upload_file(\"Table_Name\", \"rec_c8hnbch26un1nl0rthkg\", \"column_name\", \"my_avatar_bas64_encoded\")\n```\n\nNow you can download the file from your Xata database like this:\n\n```python\ndownload_response = xata.get_file(\"Table_Name\", \"rec_c8hnbch26un1nl0rthkg\", \"column_name\")\n```\n\n## 8. Transactions\n\nIf you want to perform multiple operations on your Xata database as a single unit of work, you can use transactions.\nFor this, you can use the `xata.transaction()` function. It takes a list of operations as an argument and returns a response object.\n\nSuppose you want to insert a record and update another record in your Xata database as a single unit of work.\nYou can do it like this:\n\n```python\ntransaction_response = xata.transaction([\n{\"insert\": {\"table\": \"Table_Name\", \"record\": {\"name\": \"Marie Doe\", \"age\": 21, \"email\": \"marie@mail.com\"}}}\n{\"update\": {\"table\": \"Table_Name\", \"id\": \"rec_c8hnbch26un1nl0rthkg\", \"fields\": {\"age\": 31}}}\n{\"get\": {\"table\": \"Table_Name\", \"id\": \"rec_c8hnbch26un1nl0rthkg\",\"columns\": [\"name\", \"age\"]}}\n{\"delete\": {\"table\": \"Table_Name\", \"id\": \"rec_c8hnbch26un1nl0rthkg\"}}\n])\n```\n\nNote that the `transaction()` function takes a list of operations as an argument. Each operation is a dictionary that contains\nthe type of operation and the operation-specific arguments. The supported operations are `insert`, `update`, `get`, and `delete`.\n\n## 9. SQL Queries\n\nIf you feel more comfortable with SQL, you can use the `xata.sql_query()` function to query your Xata database using SQL.\n\nThe `xata.sql_query()` function takes a SQL query as an argument and returns a response object with the query results.\nFor example, suppose you want to query your Xata database using SQL. You can do it like this:\n\n```python\nsql_response = xata.sql_query(\"SELECT * FROM Table_Name\")\n```\n\n## 10. Asking to the AI Assistant\n\nXata has an AI assistant that can help you with your queries. You can use the `xata.askai()` function to ask a question to the AI assistant,\ntaking a reference to the table you want to query and the question you want to ask as arguments.\n\nSuppose you want to ask the AI assistant to find the people in your table whose age is greater than 30. You can do it like this:\n\n```python\nresponse = xata.askai(\"Table_Name\", \"Find the people whose age is greater than 30\")\n```\n\nThe response looks like this:\n\n```json\n{\n  \"answer\": \"< answer >\",\n  \"sessionId\": \"cg52bk1eqh5rd5hndhq95jercs\",\n  \"records\": [\n    \"b70d541d114ff54ad15915636450663f\",\n    \"8ae4837002e21f013aa85c30a126ea1c\",\n    \"4b137344a3c53d5152c45ed514188cd2\"\n  ]\n}\n```\n\n---\n\nThe `st_xata_connection` package is a powerful tool that makes it easy to connect your Streamlit app to your Xata database.\nIn this article, we've covered the basics of using `st_xata_connection` to query your Xata database, insert, update, and delete data,\nwork with files, perform transactions, and ask questions to the AI assistant. But there's a lot more you can do with `st_xata_connection`.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Streamlit Xata Data Base ConnectionAn easy way to connect your Streamlit application to your Xata database.",
    "version": "1.0.3",
    "project_urls": {
        "Homepage": "https://github.com/SergioSKA27/st-xatadb-connection"
    },
    "split_keywords": [
        "streamlit",
        "xata",
        "connection",
        "integration",
        "database"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "229ad76ac1327be775caa72e10752cf2df8bd3ccac5faf21deca8a442faf75d2",
                "md5": "2487f5512b69c7f47110efa20ee25e72",
                "sha256": "67af9ba4c80c2acb52abcaac0357247a1bbd55e5ae7986f19b75dafab49f73da"
            },
            "downloads": -1,
            "filename": "st_xatadb_connection-1.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2487f5512b69c7f47110efa20ee25e72",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 11221,
            "upload_time": "2024-01-04T11:00:51",
            "upload_time_iso_8601": "2024-01-04T11:00:51.506617Z",
            "url": "https://files.pythonhosted.org/packages/22/9a/d76ac1327be775caa72e10752cf2df8bd3ccac5faf21deca8a442faf75d2/st_xatadb_connection-1.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "026f07a142f69dc7bff5491e019f54fb942f3e6cd9021fdd049d91679441310d",
                "md5": "9b937cdd118de319214f22d440831ee7",
                "sha256": "9c582acd405eed94467168ff74bb1f052c28858db0738df8fa4cd7b545969e6d"
            },
            "downloads": -1,
            "filename": "st-xatadb-connection-1.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "9b937cdd118de319214f22d440831ee7",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 14008,
            "upload_time": "2024-01-04T11:00:53",
            "upload_time_iso_8601": "2024-01-04T11:00:53.220812Z",
            "url": "https://files.pythonhosted.org/packages/02/6f/07a142f69dc7bff5491e019f54fb942f3e6cd9021fdd049d91679441310d/st-xatadb-connection-1.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-04 11:00:53",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "SergioSKA27",
    "github_project": "st-xatadb-connection",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "st-xatadb-connection"
}
        
Elapsed time: 0.16851s