Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! Check out the docs.

Python->CX_Oracle + Dash -> Tables

Hi all

Looking if someone can share a example and safe me some time.

Am a noob at Python, well make that development… Haven’t written code for a good 15yrs,
Been asked to create a dashboard,
My data comes from a Oracle Database, accessed via CX_Oracle, Currently I’m using FLASK and old style div+

Want to change this to a DASH table.

Anyone have some code/examples they willing to share.


… might have found the perfect example, and it happens to be from a friend of mine…



1 Like

And some examples how to inject bind variables into cx_Oracle/sql query exposed via a pandas dataframe:

Bind variables -

import cx_Oracle
import pandas
connection = cx_Oracle.connect('username/pwd@host:port/dbname')
def read_query(connection, query):
cursor = connection.cursor()
    cursor.execute( query )
    names = [ x[0] for x in cursor.description]
    rows = cursor.fetchall()
    return pandas.DataFrame( rows, columns=names)
    if cursor is not None:

Or (

import pandas as pd
import as psql
import cx_Oracle as odb
conn = odb.connect(_user +'/'+ _pass +'@'+ _dbenv)
	sqlStr = """SELECT * FROM customers 
            WHERE id BETWEEN :v1 AND :v2
pars = {"v1":1234, "v2":5678}
df = psql.frame_query(sqlStr, conn, params=pars)


curs = conn.cursor()
curs.execute(sqlStr, params)
df = pd.DataFrame(curs.fetchall())
df.columns = [rec[0] for rec in curs.description]

Or (

import pandas as pd
import cx_Oracle
## db parameters
user_name = 'myusernamehere'
password = 'mypasswordhere'
query = 'select * from mydbdb.table_name where rownum < 20 and creation_date > sysdate - 2'
## connect to the DW and query the data
dsn = cx_Oracle.makedsn('Thehostname', portnumber, 'Thedbname')
con = cx_Oracle.connect(user_name, password, dsn)
curs = con.cursor()

## Convert the cx_Oracle object to pandas dataframe
headers = [ x[0] for x in curs.description]
data = curs.fetchall()
df = pd.DataFrame( data, columns=headers)
## display the dataframe
## The type conversion is also pretty accurate