首页 > 解决方案 > 如何在 Streamlit 上运行 Bokeh 服务器

问题描述

我想在 Streamlit 上显示一个交互式散景图,但我无法在图中添加小部件。带有 show(p) 命令的绘图显示在选项卡上,但我无法显示小部件。Bokeh serve show 命令完美运行,但我如何在 Streamlit 中使用它。

import pandas as pd
import streamlit as st
from bokeh.models import ColumnDataSource, Select
from bokeh.plotting import figure,ColumnDataSource, show
from bokeh.models import FactorRange
from bokeh.models import NumeralTickFormatter
from math import radians
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, Grid, LinearAxis, Plot, VBar
from bokeh.io import curdoc, show
from bokeh.models import HoverTool
from bokeh.models.widgets import Slider
from bokeh.layouts import column, row
import streamlit


 st.title("Hello world!")

 uploaded_file = st.file_uploader("Choose a file")
 if uploaded_file is not None:
    df = pd.read_excel(uploaded_file)


    df = pd.read_excel("C:/Users/Files/Data.xls")
    df1 = df.groupby(['A','B'])['C'].sum().reset_index()
    #st.write(df1)
    df2 = df1.sort_values(by=['B'], ascending=False)

    st.write(df2)


    data_source = ColumnDataSource(df2)

    x = 10

    range = FactorRange(factors=list(df2.iloc[:x]['A'].unique()))

      p = figure(x_range = range ,plot_height=600,title="Top Spend by 
      Vendors",toolbar_location=None, plot_width=1200)
      r = p.vbar(x='A', top='B', width=0.04, color ='red', source=data_source)
      p.yaxis.formatter=NumeralTickFormatter(format="$0,0")
      p.xaxis.major_label_orientation=radians(60)


      def update_plot(attr, old, new):    
         label = "Siddharth"
         v_custom = streamlit.slider(label,10,30,5, step=1)
         st.write('Values:', v_custom)
         range = FactorRange(factors=list(df2.iloc[:v_custom]['Supplier'].unique()))
         Currnew = ColumnDataSource(df2[['A', 'B']])
         p.x_range.factors = list(df_filter1['Vendor'].unique())
          r.data_source.data = Currnew.data

        v_custom.on_change('value', update_plot)

标签: pythonbokehstreamlit

解决方案


在 streamlit 中使用时散景 2.0 损坏。使用早期版本在streamlit中使用bokeh


推荐阅读