What in the Python?
Tags: tutorial, python, opensource, showdev
Published: 2023-05-05 00:24:00

## TLDR;
This tutorial explains how to use the [Apollo SDK](https://github.com/apolloapi/apolloapi/) Python package to connect to an external database, transform data using Pandas, and perform Natural Language Processing on the data. With just a few lines of code, this powerful SDK provides a flexible interface for working with data and integrating with other services.

**Prerequisites:**
- Have a [supabase instance](https://supabase.com/) spun up with data 
- Create an [API token](https://docs.apolloapi.io/docs/api/authentication) with Apollo API
- A copy of your database connection string in URI format

# Quickstart

Lets begin!

## Setting Up a Connection to Supabase
First, let's install the Apollo SDK Python package using pip:

```python
pip install apollo-sdk
```

Once the package is installed, we can use it to set up a connection to [Supabase](https://supabase.com/), a popular database management tool. Here's an example code snippet that shows how to connect to Supabase using the Apollo SDK:

```python
# import the package
from apollo.client import Apollo

# sync data from your database instance (we support supabase at the current moment or postgresql via uri format)
Apollo.connect("postgres://username:password@hostname:port/database_name")

# If you want to test out operation on your external connection
Apollo.fetch_tables()

# Query the table in desc or asc order
Apollo.query("desc", "table", "column")
```

This code imports the Apollo module from the `apollo.client` package and uses the `Apollo.connect()` method to establish a connection to a Supabase database instance. Once the connection is established, we can use the `Apollo.query()` method to run queries against the database and fetch data.

## Transforming Data Using Pandas
Once we have fetched data from the external database, we can use Pandas to transform the data into a table. Here's an example code snippet that shows how to do this:

```python
import pandas as pd

# the data returned by the Apollo.query() method
data = [...] # replace with the actual data

# convert the data into a Pandas DataFrame
df = pd.DataFrame(data)

# return the first 5 rows of the DataFrame
output = df.head()

# print the output to the console
print(output)
```
*ex. output of dataframe for us using reddit data*


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0hew2na831m39793avjv.png)

This code uses the Pandas package to transform the data returned by the `Apollo.query()` method into a table. We first import the pandas module and create a Pandas DataFrame from the data using the `pd.DataFrame()` method. We then use the `head()` method to return the first 5 rows of the DataFrame.

## Sending Data to Apollo's AI model

Finally, we can use the Apollo SDK to send the data to a content API for further processing against an AI model. Here's an example code snippet that shows how to do this:

```python
# import the necessary modules
from apollo.client import Apollo

# set up the connection to the content API
Apollo.use("apollo", token="YOUR_API_TOKEN_HERE")

# select the 'text' column from the DataFrame
texts = df['text']

# loop through each text in the 'text' column and send it to the content API
for text in texts:
    # send the text to the content API to detect any threats
    result = Apollo.detectText(text, "contains", "Threats")
    
    # print the result to the console
    print(result)
```

This code sets up the connection to a content API using the `Apollo.use()` method and provides the necessary API token. We then select the text column from the Pandas DataFrame and loop through each text in the column. We use the `Apollo.detectText()` method to send each text to the content API and detect any potential threats.

*ex. output*

```
    status: 200,
        data: {
            actionsTriggered: [
            {
                id: 'ks93-as3',
                name: 'Mute User'
            },
            {
                id: 'f2-xa03d',
                name: 'Escalate Content'
            }
        ]
    }
}
```

# Conclusion

This tutorial explains how to use the Apollo SDK Python package to connect to an external database, transform data using Pandas, and utilize an AI model for NLP. With just a few lines of code, this powerful SDK provides a flexible interface for working with data and integrating with other services.

**Useful links:**
- [Docs](https://docs.apolloapi.io/)
- Collaborate with us on [Github](https://github.com/apolloapi/)
- Join our [discord](https://discord.gg/ZUH7f7AzUY/)
- Checkout the [website](https://apolloapi.io)
- Our [newsletter](https://www.thebriefnewsletter.com)