Giter VIP home page Giter VIP logo

st-xatadb-connection's Introduction

st-xatadb-connection

Xata Data Base Connection to Streamlit

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.

๐Ÿ“„ Package documentation: Streamlit App

๐ŸŒ Learn More: Xata Docs

Why Use st-xatadb-connection ๐Ÿค”?

  1. ๐Ÿ”„ Seamless Integration:

    • Effortlessly connect and interact with your Xata database using intuitive methods.
  2. ๐Ÿ› ๏ธ Comprehensive Functionality:

    • Enjoy a rich set of functions, from basic queries to advanced AI interactions and file handling.
  3. ๐Ÿ”’ Secure Credentials Handling:

    • Safely manage your Xata API key and database URL using Streamlit's secrets manager or environment variables.
  4. ๐Ÿš€ Boosted Productivity:

    • Streamline your workflow with efficient data manipulation and advanced search capabilities.
  5. ๐ŸŒ Real-time Connectivity:

    • Experience real-time interactions with your Xata database, keeping your data up-to-date.
  6. ๐Ÿง  AI Integration Made Easy:

    • Pose questions to AI models effortlessly and engage in dynamic conversations with askai and askai_follow_up functions.
  7. ๐Ÿ“„ File Handling Simplified:

    • Manage files seamlessly, including uploads, retrievals, and deletions with dedicated functions.
  8. ๐Ÿ“ˆ Data Aggregation and Summarization:

    • Aggregate and summarize data effortlessly, gaining insights at your fingertips.
  9. ๐ŸŒŸ Advanced Schema Management:

    • Dynamically create, delete, and modify tables and columns with ease.
  10. โšก๏ธ Efficient Bulk Operations:

    • Leverage bulk processing and transactions for lightning-fast database operations.
  11. ๐Ÿ–ผ๏ธ Image Transformation Capabilities:

    • Transform images on-the-fly with the image_transform function.
  12. ๐ŸŒ Pagination for Large Datasets:

    • Navigate through large datasets seamlessly with next_page and prev_page functions.
  13. ๐Ÿš€ Developer-Friendly:

    • Designed for developers by developers, ensuring a smooth and intuitive experience.

Features

๐Ÿ” Data Manipulation Made Easy

methods
  • query: Execute queries on specific tables with precision.
  • get: Retrieve records using record IDs.
  • insert: Seamlessly insert records into tables.
  • upsert: Replace records with ease.
  • update: Effortlessly update records in your tables.
  • delete: Remove records effortlessly.

๐Ÿ”Ž Advanced Search Capabilities

methods
  • search: Discover specific queries in a branch.
  • search_on_table: Uncover data in a table using a search query.
  • vector_search: Harness the power of vector searches.

๐Ÿ“Š Data Aggregation and Summarization

methods
  • aggregate: Aggregate data based on queries.
  • summarize: Summarize data in tables with precision.

๐Ÿ”„ Transaction Management

methods
  • transaction: Perform transactions effortlessly.
  • sql_query: Execute SQL queries seamlessly.

๐Ÿง  AI Integration

methods
  • askai: Pose questions and receive AI-driven responses.
  • askai_follow_up: Engage in dynamic conversations with AI models.

๐Ÿ“ File Handling Made Simple

methods
  • upload_file: Upload files to specific tables and records.
  • get_file: Retrieve files with ease.
  • delete_file: Effortlessly remove files from your database.

๐Ÿ–ผ๏ธ Image Transformation

methods
  • image_transform: Transform images effortlessly.

๐Ÿ“„ Pagination and Schema Management

methods
  • next_page: Retrieve the next page of results.
  • prev_page: Access the previous page of results.
  • get_schema: Explore table schemas dynamically.

๐Ÿ› ๏ธ Database Administration

methods
  • create_table: Create new tables effortlessly.
  • delete_table: Remove tables with precision.
  • create_column: Add new columns seamlessly.
  • delete_column: Effortlessly remove columns.
  • get_columns: Retrieve table columns dynamically.

๐Ÿš€ Bulk Operations and API Requests

methods
  • bulk_processor: Facilitate bulk processing effortlessly.
  • bulk_transaction: Manage bulk transactions seamlessly.

Getting Started with st_xata_connection

1. Set up your Xata.io and Streamlit Environment

  • Create a Xata account and database at Xata Web.

  • 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.

XATA_API_KEY = "YOUR_XATA_API_KEY"

XATA_DB_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.

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.

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.

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.

record = {
    "name": "John Doe",
    "age": 30,
    "email": "[email protected]"
}
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:

{
      "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.

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:

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:

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:

transaction_response = xata.transaction([
{"insert": {"table": "Table_Name", "record": {"name": "Marie Doe", "age": 21, "email": "[email protected]"}}}
{"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:

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:

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

The response looks like this:

{
  "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.

st-xatadb-connection's People

Contributors

philkra avatar sergioska27 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

philkra

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.