# PythonCode

Allows for custom Python code to be executed against tables and objects inside an Analytics Workflow. One can ingest any number of sources and output any number of objects.

<figure><img src="/files/S7Y87qMRL7traSyPPB8o" alt=""><figcaption></figcaption></figure>

### Input, Output

| Input                                                      | Output                                                        |
| ---------------------------------------------------------- | ------------------------------------------------------------- |
| No requirements, any number of tables or inputs is allowed | Any python altered data frames or charts dictated by `return` |

### Accessing Objects & Parameters

Any source linked to the Python code tool is accessible using the `sources` dictionary, and objects can be accessed by their index or by their name. For example:

```
df1 = sources[0].df        # access table by index
df1 = sources["foo"].df    # access table by name
```

Similarly, workflow parameters are available using the `variables` dictionary. It's recommended to access parameter by their name:

```
foo = variables["foo"]
```

### Returning Objects

Analytics allows the return of `Table` or `Chart` objects for use in downstream tools. Objects can be returned as a single object or an array of objects.

### Table

To create and return a table, provide a `name` and pandas `df` attribute to the `Table` object.

```
import pandas as pd
tbl = sources[0]
df = tbl.df 
return [Table(df=df, name="DataFrame")]
```

### Chart

To create and return a chart, provide a `name` and plotly `figure` attribute to the `Chart` object.

```
import plotly.graph_objects as go

fig = go.Figure(
    data=[go.Bar(x=[1, 2, 3], y=[1, 3, 2])],
    layout=go.Layout(
        title=go.layout.Title(text="A Figure Specified By A Graph Object")
    )
)

return Chart(name="foo", figure=fig)
```

### [Available Libraries](#user-content-fn-1)[^1]

Within the Code tool, the following libraries are accessible by default. Import libraries using `import`:

| Library                                                           | Description                                                                                                                    | Import                   |
| ----------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ | ------------------------ |
| [**Pandas**](https://pandas.pydata.org/)                          | Data manipulation                                                                                                              | `import pandas`          |
| [**Requests**](https://requests.readthedocs.io/en/master/)        | API requests                                                                                                                   | `import requests`        |
| [**Numpy**](https://numpy.org/)                                   | Numerical and statistical operations                                                                                           | `import numpy`           |
| [**URLlib**](https://docs.python.org/3/library/urllib.html)       | URL Encoding/decoding                                                                                                          | `import urllib`          |
| [**JSON**](https://docs.python.org/3/library/json.html)           | JSON handling                                                                                                                  | `import json`            |
| [**Python-dateutil**](https://dateutil.readthedocs.io/en/stable/) | Provides powerful extensions to the standard datetime module                                                                   | `import python-dateutil` |
| [**Xlrd**](https://xlrd.readthedocs.io/en/latest/)                | Reading data and formatting information from Excel files                                                                       | `import xlrd`            |
| [**Openpyxl**](https://openpyxl.readthedocs.io/en/stable/)        | Excel file reads                                                                                                               | `import openpyxl`        |
| [**Statsmodels**](https://www.statsmodels.org/stable/index.html)  | Provides classes and functions for the estimation of many different statistical models                                         | `import statsmodels`     |
| [**Scipy**](https://pypi.org/project/scipy/)                      | Statistics, optimization, integration, linear algebra, Fourier transforms, signal and image processing, ODE solvers, and more. | `import scipy`           |
| [**Scikit-Learn**](https://pypi.org/project/scikit-learn/)        | Machine Learning                                                                                                               | `import sklearn`         |
| [**Msal**](https://learn.microsoft.com/en-us/entra/msal/python/)  | Microsoft Authentication Library                                                                                               | `import msal`            |

[^1]:


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.auditboardanalytics.com/tools/code/pythoncode.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
