# User input that updates an SQL Query

**URL:** <https://community.plotly.com/t/user-input-that-updates-an-sql-query/7650>\
**Category:** Dash Python\
**Created:** [January 10, 2018, 2:24am UTC](https://community.plotly.com/t/user-input-that-updates-an-sql-query/7650 "2018-01-10T02:24:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gravesmcgavon](https://avatars.discourse-cdn.com/v4/letter/g/e79b87/32.png) [@gravesmcgavon](https://community.plotly.com/u/gravesmcgavon)\
**Post date:** [January 10, 2018, 2:24am UTC](https://community.plotly.com/t/user-input-that-updates-an-sql-query/7650/1 "2018-01-10T02:24:37Z")

</div>

Currently I have a database connected to dash. A query is run, put into a dataframe and put into my graphs. i need the graphs to be able to run a different query for different graphs. Right now I am just updating variables that formats the string that the query is contained in.

What is the best way for the user to update the query? I was trying a radio button but it seems like the output of that radio button needs to be returned somewhere inside the layout. Is there a way to have the radio button’s ‘value’ update a variable so it will insert that value into the query and run again?

What would a callback look like for something like this?

I am posting some stripped down version of my code without any callback at the bottom. Does anyone have an idea of the best way to do something like this in the most “DRY” way and least taxing?  
Thanks in advance!!

```
server = 'server' 
database = 'database' 
username = 'user' 
password = 'password' 
conn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = conn.cursor()

format_select = 'format1'
table_select = 'table1'

sql = '''

SELECT {} FROM {}

'''.format(format_select, table_select)

df = pandas.io.sql.read_sql(sql, conn)

conn.close()

status_tracker = df.Digit_Status
Counter(status_tracker)

app = dash.Dash()

app.layout=html.Div([
            
      dcc.Checklist(
              id = 'checklist',
    options=[
        {'label': 'format1', 'value': 'format1'},
        {'label': 'format2', 'value': 'format2'},
        {'label': 'format3', 'value': 'format3'}
    ],
    values=['format1']
),      
    
     dcc.Graph(
            id='Status Tracker K7',
            figure={
                'data': [
                    {'x': Counter(status_tracker).keys(), 
                     'y': Counter(status_tracker).values(),
                     'type': 'bar',  
                     'width': .05},
                    
                ],
                'layout': {
                    'title': 'Status Tracker K7',
                    'width': '50%',   
                }},
                    style={'border': '5px solid maroon', 
                    'margin': '30px',
                    'width': '700px',
                    'float': 'left'}
            )
            
            )),
    
])

if __name__ == ' __main__':
    app.run_server(debug=True)
```

---

<div class="post-metadata">

**Author:** ![nedned](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/nedned/32/8795_2.png) [@nedned](https://community.plotly.com/u/nedned)\
**Post date:** [January 10, 2018, 4:06am UTC](https://community.plotly.com/t/user-input-that-updates-an-sql-query/7650/2 "2018-01-10T04:06:59Z")

</div>

The general strategy I think you want to use is have the query generated within a callback that is listening for the user inputs. So perhaps something like this might work, assuming a function `run_query_and_get_output` which executes an string containing an SQL query against a DB and prepares the output you need:

```python
@app.callback(Output('target', 'children'), [Input('checklist', 'values')])
def run_query(values):
    sql = "SELECT {} from some_table".format(','.join(values))
    return run_query_and_get_output(sql)

```

---

<div class="post-metadata">

**Author:** ![gravesmcgavon](https://avatars.discourse-cdn.com/v4/letter/g/e79b87/32.png) [@gravesmcgavon](https://community.plotly.com/u/gravesmcgavon)\
**Post date:** [January 19, 2018, 2:44am UTC](https://community.plotly.com/t/user-input-that-updates-an-sql-query/7650/3 "2018-01-19T02:44:26Z")

</div>

Thanks a lot for your quick reply! It took me a while to get back to this project but I have taken what you said into consideration and was able to get the code to do what I need it to do!

Thank you sooo much for your help. I’m sure I will have more questions as I proceed through this project. I truly appreciate everyone’s care and responsiveness in this forum!

Thanks again.

---

<div class="post-metadata">

**Author:** ![liotundi](https://avatars.discourse-cdn.com/v4/letter/l/7bcc69/32.png) [@liotundi](https://community.plotly.com/u/liotundi)\
**Post date:** [December 19, 2019, 12:29am UTC](https://community.plotly.com/t/user-input-that-updates-an-sql-query/7650/4 "2019-12-19T00:29:54Z")

</div>

Did you ever deploy this? if yes how did you plug in those sql credentials into the app?
