✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🧬 Learn how to build RNA-Seq data apps with Python & Dash. Register for the May 20 Webinar!

Connect to OracleDB with a User Form and share connection between callbacks

Hi,

I wanted to create a user form, so one can conenct to an Oracle Database using cx_Oracle. I am able to connect to the database without problem and query data within a callback, but I am not quite sure how to “share” the open connection between different callbacks to make different queries or to close the connection with a logout button.

Any advice very appreciated! Thanks!

app.layout = html.Div([
    html.Header([
        html.Img(src=comp_img, className='header-img'),
        html.H1('My Project')
    ]),
    html.Div([
        html.Form([
            dcc.Input(id='user-name', placeholder='Username', type='text'),
            dcc.Input(id='user-pw', placeholder='Passwort', type='password'),
            html.Button('Login', id='submit-button', type='submit', className='myButton'),
            html.Div(id='intermediate-value')
        ], className='LoginForm')
    ], className='container'),
])
@app.callback (
    Output('intermediate-value', 'children'),
    [Input('submit-button', 'n_clicks')],
    [State('user-name', 'value'),
     State('user-pw', 'value')]
)
def connect_db(n_clicks, username, password):
    if n_clicks is None:
        return
    else:
        print(n_clicks)

    try:
        my_connection = cDB.user_input(username, password)
        qry = """SELECT * FROM d_fakten"""
        df = cDB.query_tbl(qry, my_connection)

        print(df)

    except Exception as e:
        print(e)