# Python

### 1. Setup

The Python query runner lets you run arbitrary Python 3 scripts and visualize the contents of a `result` variable declared in the script. For security, is disabled by default.

You can create a Python data source from **Settings > Data Sources**.

* **Modules to import prior to running the script** lets you define which modules that were installed by `pip` on the host server may be import in the application queries.
* **AdditionalModulesPaths** is a comma-separated list of absolute paths *on the server* to Python modules that should be available when querying from the application. This is useful for private modules that are not available from `pip`.
* **AdditionalBuiltins** the application automatically allows twenty-five of Python’s built-in functions that are considered safe. You can specify others here.

These are the default built-ins: `abs`, `all`, `any`, `bool`, `complex`, `dict`, `divmod`, `enumerate`, `filter`, `float`, `int`, `len`, `list`, `map`, `max`, `min`, `next`, `reversed`, `round`, `set`, `slice`, `sorted`, `str`, `sum`, `tuple`

Furthermore, please note that certain basic modules such as json, web3, pandas, requests, and logging are also pre-loaded to facilitate the execution of your scripts.

### 2. Writing Queries

The application builds the result table by inspecting the final state of your script execution for a variable named `result`.

`result` should follow this example format:

```python
result = {
  "columns": [
    {
      "name": "date",
      "type": "date",
      "friendly_name": "date"
    },
    {
      "name": "day_number",
      "type": "integer",
      "friendly_name": "day_number"
    },
    {
      "name": "value",
      "type": "integer",
      "friendly_name": "value"
    },
    {
      "name": "total",
      "type": "integer",
      "friendly_name": "total"
    }
  ],
  "rows": [
    {
      "value": 40832,
      "total": 53141,
      "day_number": 0,
      "date": "2014-01-30"
    },
    {
      "value": 27296,
      "total": 53141,
      "day_number": 1,
      "date": "2014-01-30"
    },
    {
      "value": 22982,
      "total": 53141,
      "day_number": 2,
      "date": "2014-01-30"
    }
  ]
}
```

If you execute the above snippet, it will return this table:

| date       | day\_number | value | total |
| ---------- | ----------- | ----- | ----- |
| 2014-01-30 | 0           | 40832 | 53141 |
| 2014-01-30 | 1           | 27296 | 53141 |
| 2014-01-30 | 2           | 22982 | 53141 |


---

# 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.inverse.watch/user-guide/data-sources/python.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.
