How to draw a graph from a SQL request using Dash?

I have been searching but I didn’t find a simple way to draw a graph from a SQL request.

For example I have this code, and I want to make a bar chart from the result of the request :

import pymysql as sql
from dash import dcc

DB = …
HOST = …
USER = …
PASSWORD = …

connection = sql.connect(host=HOST,
port=x,
user=USER,
password=PASSWORD,
database=DB,
cursorclass=sql.cursors.DictCursor)

with connection.cursor() as cursor:
# Read a single record
sql = ‘SELECT COUNT(*) AS count FROM table’
cursor.execute(sql)
result = cursor.fetchone()
print(result)

Also, I would like to update the chart regularly.

Thank you

Hi,

Welcome to the community! :slightly_smiling_face:

You can use dcc.Interval to trigger updates at every n seconds: Live Updates | Dash for Python Documentation | Plotly

Then what you need to do is basically take the code that you wrote, add it to the callback and use the value to update the chart. The fastest approach is to use extendData, as explained in this old post: What is a correct usage of extendData property to achieve adding data to existing graph instead of constantly updating it? - #4 by Emmanuelle

Please let us know if you have more specific questions about this use case.

Hope this helps!