Mightybundy
Mightybundy

Reputation: 55

How to update Pretext in Bokeh with a Select tool

I have a bokeh plot that updates my plot through a select tool. The select tool contains subjects that update the plot where the values are x='Polarity'and y='Subjectivity'.

Here is a dummy data for what I want:

import pandas as pd
import random

list_type = ['All', 'Compliment', 'Sport', 'Remaining', 'Finance', 'Infrastructure', 'Complaint', 'Authority',
 'Danger', 'Health', 'English']



df = pd.concat([pd.DataFrame({'Subject' : [list_type[i] for t in range(110)], 
                   'Polarity' : [random.random() for t in range(110)],
                   'Subjectivity' : [random.random() for t in range(110)]}) for i in range(len(list_type))], axis=0)

My code for updating the plot looks like this:

options = []
options.append('All')

options.extend(df['Subject'].unique().tolist())
source = ColumnDataSource(df)

p = figure()
r = p.circle(x='Polarity', y='Subjectivity', source = source)

select = Select(title="Subject",  options=options, value="All")
output_notebook()

def update_plot(attr, old, new):
    if select.value=="All":
        df_filter = df.copy()
    else:
        df_filter = df[df['Subject']==select.value]
    source1 = ColumnDataSource(df_filter)
    r.data_source.data = source1.data
select.on_change('value', update_plot)
layout = column(row(select, width=400), p)
#show(layout)
curdoc().add_root(layout)

I want to add a 'Pretext' that has a df.describe(), that can update with the plot through the select tool. I tried this by adding these codes but it displays nothing:

stats = PreText(text='', width=500)
t1 = select.value

def update_stats(df, t1):
    stats.text = str(df[[t1, select.value+'_returns']].describe())


select.on_change('value', update_plot, update_stats)
layout = column(row(select, width=400), p, stats)
curdoc().add_root(layout)

show(layout)

Anyone know a solution? Thanks!

Upvotes: 0

Views: 960

Answers (1)

Aritesh
Aritesh

Reputation: 2103

You don't need two separate function for that, you can just change your original function update_plot to add statement to change the text for PreText as stats.text = str(df_filter.describe()). The function will look as below -

def update_plot(attr, old, new):
    if select.value=="All":
        df_filter = df.copy()
    else:
        df_filter = df[df['Subject']==select.value]
    source1 = ColumnDataSource(df_filter)
    r.data_source.data = source1.data
    stats.text = str(df_filter.describe())

Entire code

from bokeh.models.widgets import Select, PreText
from bokeh.layouts import column, row
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, curdoc
from bokeh.plotting import figure, show
import pandas as pd
import random


list_type = ['All', 'Compliment', 'Sport', 'Remaining', 'Finance', 'Infrastructure', 'Complaint', 'Authority',
 'Danger', 'Health', 'English']



df = pd.concat([pd.DataFrame({'Subject' : [list_type[i] for t in range(110)], 
                   'Polarity' : [random.random() for t in range(110)],
                   'Subjectivity' : [random.random() for t in range(110)]}) for i in range(len(list_type))], axis=0)

options = []
options.append('All')

options.extend(df['Subject'].unique().tolist())
source = ColumnDataSource(df)

p = figure()
r = p.circle(x='Polarity', y='Subjectivity', source = source)

select = Select(title="Subject",  options=options, value="All")
#output_notebook()

stats = PreText(text=str(df.describe()), width=500)

def update_plot(attr, old, new):
    if select.value=="All":
        df_filter = df.copy()
    else:
        df_filter = df[df['Subject']==select.value]
    source1 = ColumnDataSource(df_filter)
    r.data_source.data = source1.data
    stats.text = str(df_filter.describe())


select.on_change('value', update_plot)
layout = column(row(select, width=400), p, stats)
#show(layout)
curdoc().add_root(layout)

Upvotes: 1

Related Questions