stremio中的交互式bokeh小部件

时间:2020-09-06 23:19:38

标签: python bokeh streamlit

我已经解决了这个问题一个星期,无法解决问题。我目前在Streamlit上有一段代码,在Bokeh上有一些选项卡,例如:

import bokeh
import bokeh.layouts
import bokeh.models
import bokeh.plotting

#Tabs
tabs = bokeh.models.widgets.Tabs(
    tabs=[
        results(OPT),
        graphs(OPT),
        results_2(OPT),
    ]
)
st.bokeh_chart(tabs)

只要我只显示数据就没有问题,但是我想(还有只有表格的“结果”标签)和另一个带有一些图形的标签可以过滤一些信息:

def graphs(OPT):
if OPT is not None:
    results = OPT.tabla_performance()
    source = bokeh.models.ColumnDataSource(results)
    template="""                
                <p style="font-size: 12px"> 
                    <%= (value).toFixed(2) +'%' %>
                </p>               
                """
    formatter =  bokeh.models.widgets.HTMLTemplateFormatter(template=template)        
    columns = [
        bokeh.models.widgets.TableColumn(field="Product", title="Product"),
        bokeh.models.widgets.TableColumn(field="Performance", title="Performance", formatter=formatter),
    ]
    data_table = bokeh.models.widgets.DataTable(
        source=source, columns=columns, height=280, sizing_mode="stretch_width"
    )

    text = """
    <br>Performance graph
    """
    checkbox = bokeh.models.widgets.RadioButtonGroup(labels=["Total", "By product"], active=0)
    if checkbox == "By product":
            checkbox_group = bokeh.models.widgets.CheckboxButtonGroup(labels=["Option 1", "Option 2", "Option 3"], active=[])

    column = bokeh.layouts.Column(
        _markdown(text), 
        data_table,
        checkbox,
        sizing_mode="stretch_width"
    )

return bokeh.models.Panel(child=column, title="Graphs")

我的问题出在以下几行:

    checkbox = bokeh.models.widgets.RadioButtonGroup(labels=["Total", "By product"], active=0)
    if checkbox == "By product":
            checkbox_group = bokeh.models.widgets.CheckboxButtonGroup(labels=["Option 1", "Option 2", "Option 3"], active=[])

第一个可以很好地显示两个选项,但是我无法像在普通Streamlit中那样使它具有动画效果。变量checkbox不会返回任何值,因此永远不会执行条件行。

我也尝试过使用callback选项将回调定义为:

checkbox = bokeh.models.widgets.RadioButtonGroup(labels=["Total", "By product"], active=0, callback=bokeh.models.CustomJS.from_py_func(callback))

def callback(attr, old, new):
    bokeh.models.widgets.CheckboxButtonGroup(labels=["Option 1", "Option 2", "Option 3"], active=[])

但这似乎也不起作用。我的最终目标是使用复选框组中的这些选项,以在图形上显示不同的数据或最终触发另一个功能。

有帮助/想法吗?

PS:添加最少的工作代码,目标是在“制图”选项卡中,一旦按下“按产品”按钮即可具有一个复选框列表,以便可以通过复选框选择来过滤图。

import datetime
import random

import bokeh
import bokeh.layouts
import bokeh.models
import bokeh.plotting
import markdown
import pandas as pd
import streamlit as st


def main():
st.title('Test')

tabs = bokeh.models.Tabs(
    tabs=[
        results(),
        graphs(),
    ]
)
st.bokeh_chart(tabs)


def results():
N = 10
data = dict(
    dates=[datetime.date(2014, 3, i + 1) for i in range(N)] * 100,
    downloads=[random.randint(0, 100) for i in range(N)] * 100,
)
source = bokeh.models.ColumnDataSource(data)

columns = [
    bokeh.models.widgets.TableColumn(
        field="dates", title="Date", formatter=bokeh.models.widgets.DateFormatter()
    ),
    bokeh.models.widgets.TableColumn(field="downloads", title="Downloads"),
]
data_table = bokeh.models.widgets.DataTable(
    source=source, columns=columns, height=280, sizing_mode="stretch_width"
)
column = bokeh.layouts.Column(
    children=[data_table], sizing_mode="stretch_width"
)
return bokeh.models.Panel(child=column, title="Tables")

def graphs():
chart = bokeh.plotting.figure(sizing_mode="stretch_width", height=400)
chart.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=3, color="navy", alpha=0.5)

checkbox = bokeh.models.widgets.RadioButtonGroup(labels=["Total", "By product"], active=0)
if checkbox == "By product":
        checkbox_group = bokeh.models.widgets.CheckboxButtonGroup(labels=["Option 1", "Option 2", "Option 3"], active=[])

column = bokeh.layouts.Column(
    chart,
    checkbox,
    sizing_mode="stretch_width"
)    

return bokeh.models.Panel(child=column, title="Plotting")

main()

0 个答案:

没有答案
相关问题