# Python

{% hint style="info" %}
This tool is currently in Beta and is still being tested. Want to learn more? Like to provide feedback? Please reach out to <support@cascade.io>
{% endhint %}

Code allows for custom Python to be executed against tables and objects inside a Cascade Workflow. One can ingest any number of sources and output any number of objects.

### Accessing Objects & Variables

Any source linked to the 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 variables are available using the `variables` dictionary. It's recommended to access variables by their name:&#x20;

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

### Returning Objects

Cascade allows the return of `Table,` `Chart` , `Json`, or `Markdown` 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.

```
return Table (
     name = "foo"
     df = df1
)
```

#### 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)
```

#### JSON

To create and return a json, provide a `name` and python `dict` attribute to the `JSON` object.

```
return Json(name="foo", json={"counter": 1})
```

#### Markdown

To create and return a json, provide a `name` and python `dict` attribute to the `JSON` object.

```
return Markdown(name="foo", markdown="# Hello")
```

### Available Libraries

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`               |
| [**JSON**](https://docs.python.org/3/library/json.html)                               | JSON handling                              | `import json`                   |
| [**Numpy**](https://numpy.org/)                                                       | Numerical and statistical operations       | `import numpy`                  |
| [**SciKit Learn**](https://scikit-learn.org/stable/)                                  | Regressions and machine learning           | `import sklearn`                |
| [**FB Prophet**](https://facebook.github.io/prophet/docs/quick_start.html#python-api) | Time-series forecasting                    | `from fbprophet import Prophet` |
| [**FeatureTools**](https://featuretools.alteryx.com/en/stable/)                       | Feature engineering                        | `import featuretools`           |
| [**EvalML**](https://evalml.alteryx.com/en/stable/start.html)                         | Machine learning                           | `import evalml`                 |
| [**SkTime**](https://github.com/alan-turing-institute/sktime)                         | Time series forecasting and classification | `import sktime`                 |
| [**Fuzzymatcher**](https://github.com/RobinL/fuzzymatcher)                            | Fuzzy matching for data cleaning           | `import fuzzymatcher`           |
| [**Record Linkage**](https://recordlinkage.readthedocs.io/en/latest/about.html)       | Fuzzy matching for data cleaning           | `import recordlinkage`          |
| [**Requests**](https://realpython.com/python-requests/)                               | API requests                               | `import requests`               |
| [**SciPy**](https://docs.scipy.org/doc/scipy/reference/)                              | Math and engineering                       | `import scipy`                  |
| [**Scrapy**](https://docs.scrapy.org/en/latest/)                                      | Web scraping                               | `import scrapy`                 |
| [**BeautifulSoup**](https://www.crummy.com/software/BeautifulSoup/bs4/doc/)           | Web scraping                               | `from bs4 import BeautifulSoup` |
| **URLlib**                                                                            | URL Encoding/decoding                      | `import urllib`                 |
