I want to create a search box that will get input from user, put it into an SQL query, and return matching records from the queried database.
Everything seems to work just fine - except I can't display my query results as a Pandas dataframe. The script just does when it should display the object. However, when I use print(), my result is displayed.
When I do it like this, nothing happens:
query = pd.read_sql(sql_string, connection)
query
But this works just fine:
query = pd.read_sql(sql_string, connection)
print(query)
This is my code:
import qgrid
from ipywidgets import widgets
from IPython.display import display
connection = 'connection:details'
text = widgets.Text()
display(text)
def handle_submit(a):
user_input = text.value
sp_char = "%"
sql_string = "SELECT a FROM my_database WHERE UPPER(a) LIKE UPPER('%s%s%s') % (sp_char,user_input,sp_char)
query = pd.read_sql(sql_string, connection)
query
text.on_submit(handle_submit)
I'm pretty sure I'm doing something wrong with the way that variables are moved inside and outside of the def but I'm not really sure where to go from here.
EDIT:
Solved with ipywidget function display()
query = pd.read_sql(sql_string, connection)
display(qgrid.show_grid(query))
This is all I needed :)