# Smart Charts

{% hint style="success" %}
This is the official documentation of the `forestadmin-agent-django` and `forestadmin-agent-flask` Python agents.
{% endhint %}

{% hint style="info" %}
Due to popular demand, Live Query Charts are planned to be reintroduced in a future version of the agent, but we don't have a timeline for it yet.
{% endhint %}

[Live Query Charts](https://docs.forestadmin.com/user-guide/dashboards/charts/create-a-chart#creating-a-chart-with-sql) allowed the creation of charts from SQL queries from the UI.

{% hint style="info" %}
You can find the full documentation of chart customization [here](https://docs.forestadmin.com/developer-guide-agents-python/agent-customization/charts).
{% endhint %}

## Steps

### Step 1: Retrieve the SQL query from the UI

![Live Query Chart configuration screen](https://2921382565-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HgnlEINLUAEQC1KgN48%2Fuploads%2Fgit-blob-c2b0b3237858b214e691418b5951128825a82c01%2Fmigration-chart-sql.png?alt=media)

You can retrieve the SQL query of a `Live query chart` by clicking on the `Cog` icon of the chart when using the `Edit Layout` mode.

### Step 2: Create a new API chart

The next step will be to create a new [API chart](https://docs.forestadmin.com/developer-guide-agents-python/agent-customization/charts) using the SQL query you retrieved in the previous step.

```python
from forestadmin.datasource_toolkit.context.agent_context import (
    AgentCustomizationContext
)
from forestadmin.datasource_toolkit.decorators.chart.result_builder import (
    ResultBuilder
)

def appointments_chart(
  context: AgentCustomizationContext, result_builder: ResultBuilder
):
    raw_query = """SELECT current.count AS value, previous.count AS previous
      FROM (
        SELECT COUNT(*)
        FROM appointments
        WHERE start_date BETWEEN '2018-01-01' AND '2018-02-01'
      ) as current, (
        SELECT COUNT(*)
        FROM appointments
        WHERE start_date BETWEEN '2017-12-01' AND '2018-01-01'
      ) as previous;"""

    with (
        context.datasource.get_collection("appointments").get_native_driver()
    ) as cursor:
        cursor.execute(raw_query)
        rows = cursor.fetchall()
    return resultBuilder.value(rows[0][0], rows[0][1])

agent.add_chart('appointments', appointments_chart)
```

### Step 3: Retrieve the URL of the generated chart

The url of your newly created api chart is `/forest/_charts/$chartName`. The `$chartName` is the name given to the `add_chart` method. For the previous example the url should be `/forest/_charts/appointments`

### Step 4: Change the old chart configuration

{% hint style="info" %}
If you are following this guide step by step, you are probably testing your agent on a temporary project.

You may want to postpone this step until you have replaced your old agent with the new one.
{% endhint %}

Instead of `Query`, select `Smart` in the data source selector, and enter the path that was printed to the console in the previous step.
