How to write python code inside Redash

I add python as data source to redash but when import mysql.connector module get error

def read_sql():
import mysql.connector
mydb = mysql.connector.connect(
mycursor = mydb.cursor()
mycursor.execute(“select hour,sum(temperature) as temperature ,sum(wind_speed) as wind_speed from weatherdaat group by hour”)
myresult = mycursor.fetchall()
for x in myresult:

Error running query: <type ‘exceptions.AttributeError’> ‘module’ object has no attribute 'connector’


Try to create a MySQL connection via interface. Then you can use execute_query method to run MySQL inside Python.

For example:

  1. I’ve created DataSource called “MySQL” with id = 10.
  2. I can run this code in Python query runner:
result = execute_query(
    10, # pass id or connection name = "MySQL"
    """SELECT 1 as test"""
  1. You can print result var to understand how it works.

Also there is some great functions you can use in python:

  • add_result_row()
  • add_result_column()
  • get_query_result()
  • get_source_schema()

More info here:

this isnot what i mean, i need to write pure python code inside redash is there a way for that?? @Vladis