Hi, everyone.
Could you tell me my easy question?
I want to change button color when click the button and finish treatment finish.
I think this question is easy for anyone. But I don’t know how to change it.
My simple code
from bokeh.models.annotations import Title
import hvplot.pandas # noqa
import numpy as np
import pandas as pd
import panel as pn
import param
INDICES = ["v1", "v2", "v3", "v4"]
#------------------------------------------------------------------------------------
# Definition ReactiveTable Class
#------------------------------------------------------------------------------------
class ReactiveTable(pn.viewable.Viewer):
table = param.DataFrame() # x,y table
count = param.Integer() # number of items
random = param.Action(lambda x: x.param.trigger("random"), label="Random")
x = param.ObjectSelector(INDICES[0], objects=INDICES, label="x") # selector x
y = param.ObjectSelector(INDICES[1], objects=INDICES, label="y") # selector y
table_widget = param.Parameter() # to get table_widget.selection to work in depends
def __init__(self, **params):
super().__init__(**params)
self.table_widget = pn.Param(self.param.table)[0]
# Definition click random button
@param.depends("random", watch=True, on_init=True)
def _fill_table_with_random_data(self):
size = np.random.randint(5, 15)
nums = np.random.randint(5, 15) # <--- 2021/12/06 Change nums of items
self.count = nums # <--- 2021/12/06 Display nums of items
INDICES = [ str('v'+str(i)) for i in range(nums) ] # <--- 2021/12/06 Make a new indices list
self.table = pd.DataFrame(
{i: np.random.randint(1, 100, size) for i in INDICES}
).T
self.param.x.objects = INDICES # change selector x lists
self.param.y.objects = INDICES # change selector y lists
# Definition click table widget
@param.depends("table_widget.selection", watch=True)
def _set_x_with_table_selection(self):
OBJECT = (self.param.x.objects)
if self.table_widget.selection:
self.x = OBJECT[self.table_widget.selection[0]]
if hasattr(self, "tabs") : self.tabs.active = 0 # if it is not initial , self has tabs objects. so change active tab=0
else:
self.x = OBJECT[0]
# self.count += 1 # <--- 2021/12/06 Comment out
# Definition select x value
@param.depends("x", watch=True)
def _set_table_selection_with_x(self):
OBJECT = (self.param.x.objects)
self.table_widget.selection = [OBJECT.index(self.x)]
# Definition select x,y value or click random button
@param.depends("x", "y", "random")
def plot(self):
return self.table.T.hvplot.scatter(
x=self.x, y=self.y, color="red", grid=True, xlim=(0, 100), ylim=(0, 100)
).opts(title=str(self.table[0][0]))
# Definition update data table
@param.depends("table")
def table_list(self):
return pn.pane.DataFrame(self.table, sizing_mode="fixed")
# Definition panel layout
def __panel__(self):
# Layout
graph_layout = pn.Row(
pn.Column(
pn.pane.Markdown("## Update table"),
self.param.x,
self.param.y,
self.param.random,
),
pn.panel(self.plot, sizing_mode="fixed"),
)
list_layout = pn.Column(self.table_widget, self.param.count, self.param.random)
self.tabs = pn.Tabs(
("Graph", graph_layout),
("List", list_layout),
active=self.tabs.active if hasattr(self, "tabs") else 0,
)
return pn.template.FastListTemplate(
site="Panel",
main=self.tabs,
title="Panel Sample",
theme="dark",
)
# run app
if __name__ == "__main__":
app = ReactiveTable()
app.show(port=5007)
elif __name__.startswith("bokeh"):
app = ReactiveTable()
app.servable()
Screen shot 1
When I click the random button. when graph values change, I want to change random button color.
Screen shot 2
this is the point that I want to change button color.
Screen shot 3
I checked panel document. But I couldn’t apply my button.
Button — Panel 0.12.6 documentation (holoviz.org)
I’m sorry bothering for you. But could you tell me that?