Apologies about the delay in response. But I did some more testing and have identified what scenario I’m in (not that I understand why) I was unable to print to the console. I was running ‘bokeh serve /foldername/’ trying to structure it like an app rather than ‘bokeh serve /foldername/app.py’. When I run it with the later the print statement does output to the console but it also makes the callback function associated with the button not write to the database, but if I remove the print statement the callback function writes to the database without any issue. Any idea why you can’t have both in the same function. Doesn’t matter where I put the print statement in the function, it will execute but won’t apply the changes to the database.
So the short of it is the print statement seems to break writing to my sql database and doesn’t display anything on the console when run as an application (I realise this is probably by design and I’m overlooking something with how an application folder functions in bokeh) rather than a specific script.
I’m also running the platform on Debian with kernel 3.16.4-4-amd64.
import string
import pandas as pd
from bokeh.plotting import Figure
from bokeh.models import ColumnDataSource, TextInput, Button, Panel, Tabs, Label, DataTable, TableColumn, StringEditor
from bokeh.layouts import Row, Column, widgetbox
from bokeh.io import curdoc, show, output_notebook, gridplot
from sqlalchemy import create_engine
from models.Data_Import import Import_Statements
connection string adjusted to reflect structure only for this post
engine = create_engine(‘postgresql+psycopg2://user:[email protected]:5432/Test_app’)
def retreive_categories():
df = pd.read_sql(sql=‘SELECT DISTINCT category from public.“Description_Categories” WHERE category<>‘Unknown’’, con=engine)
if len(df) == 0:
return
return list(df[‘category’].unique())
def retreive_desc_cat():
df = pd.read_sql(sql=‘SELECT description, category from public.“Description_Categories” WHERE category=‘Unknown’’, con=engine)
cds = ColumnDataSource(df)
cds.remove(‘index’)
columns = [TableColumn(field=‘description’, title=‘Description’),TableColumn(field=‘category’, title=‘Category’, editor=StringEditor(completions=retreive_categories()))]
tab_desc_cat = DataTable(source=cds, columns=columns, editable=True)
return tab_desc_cat
def cb_update_category():
conn = engine.connect()
df = category_update.source.to_df()
df_write = df[df[‘category’] != “Unknown”]
for r in df_write.iterrows():
sql_comm = ‘’‘UPDATE public.“Description_Categories” SET category = %s WHERE description = %s’’’
conn.execute(sql_comm, (string.capwords(r[1][0]),r[1][1]))
conn.close()
new_data = df[df[‘category’] == ‘Unknown’]
category_update.source.data = {‘description’: new_data[‘description’], ‘category’: new_data[‘category’]}
print(“Applying Updates to DB”)
category_update = retreive_desc_cat()
apply_button = Button(label=“Apply Changes”)
apply_button.on_click(cb_update_category)
tab1 = Panel(child=Column(apply_button,category_update), title=‘Category Updates’)
layout = Tabs(tabs=[tab1])
curdoc().add_root(layout)