Python (Datapane) : 如何将动态变量传递到数据窗格报表函数中

Python (Datapane) : How to pass dynamic variables into a datapane report function

我正在开发一个图表模块,我可以在其中传递数据框,该模块将根据调用下面提到的几个函数生成的图表创建报告。

我使用 Altair 绘图,使用“Datapane”创建报告,可以在此处找到相同的文档:https://datapane.github.io/datapane/

我的 DataFrame 看起来像这样

d = {'Date': ['2021-01-01', '2021-01-01','2021-01-01','2021-01-01','2021-01-02','2021-01-03'], 
     'country': ['IND','IND','IND','IND','IND','IND' ],
     'channel': ['Organic','CRM','Facebook','referral','CRM','CRM' ],
     'sessions': [10000,8000,4000,2000,7000,6000 ],
     'conversion': [0.1,0.2,0.1,0.05,0.12,0.11 ],
      }
country_channel = pd.DataFrame(d)

绘图函数:

def plot_chart(source,Y_axis_1,Y_axis_2,chart_caption):
    base = alt.Chart(source).encode(
    alt.X('Date:T', axis=alt.Axis(title="Date"))
    )

    line_1 = base.mark_line(opacity=1, color='#5276A7').encode(
    alt.Y(Y_axis_1,
    axis=alt.Axis( titleColor='#5276A7'))
    )

   line_2 = base.mark_line(opacity=0.3,color='#57A44C', interpolate='monotone').encode(
    alt.Y(Y_axis_2,
          axis=alt.Axis( titleColor='#57A44C'))
   )

   chart_ae=alt.layer(line_1, line_2).resolve_scale(
    y = 'independent'
   ).interactive()

   charted_plot = dp.Plot(chart_ae , caption=chart_caption)
   return  charted_plot

def channel_plot_split(filter_1,filter_2,country,channel):
    channel_split_data = country_channel[(country_channel[filter_1]==country.upper())]
    channel_split_data =channel_split_data[(channel_split_data[filter_2].str.upper()==channel.upper())]
    channel_split_data=channel_split_data.sort_values(by='Date',ascending = True)
    channel_split_data=channel_split_data.reset_index(drop=True)
    channel_split_data.head()

    plot_channel_split = plot_chart(source=channel_split_data,Y_axis_1='sessions:Q',Y_axis_2='conversion:Q',chart_caption="Sessions-Conversion Plot for Country "+country.upper()+" and channel :"+ channel)
    channel_plot=dp.Group(dp.HTML("<div class='center'> <h3> Country : "+country.upper()+" & Channel : "+channel.upper()+"</h3></div>"),plot_channel_split,rows=2)
    return channel_plot

def grpplot(plot_1,plot_2):
    gp_plot = dp.Group(plot_1,plot_2,columns=2)
    return gp_plot

调用上述函数时,将过滤数据框,为每个过滤器创建图,并将 2 个图连续分组。

row_1 = grpplot(channel_plot_split('country','channel','IND','Organic'),channel_plot_split('country','channel','IND','CRM'))
row_2 = grpplot(channel_plot_split('country','channel','IND','Facebook'),channel_plot_split('country','channel','IND','referral'))

我现在可以通过调用 datapane.Report() 函数生成报告,如下所示

r= dp.Report(row_1,row_2)

问题: 当我知道有多少频道存在时,这工作正常,但我的频道列表是 dynamic.I 我使用“for”循环生成行,但不确定如何在 dp.Report() 函数中将这些行作为 kwargs 传递。比如我有10个频道,我需要动态传10行。

我遇到了类似的问题,解决方法如下

  1. 创建一个列表来存储报表的页面或元素,例如
    • report_pages=[]
    • report_pages.append(dp.Page)
    • report_pages.append(dp.Table)
    • report_pages.append(dp.Plot)
  2. 最后只生成带有指向列表的指针的报告
    • dp.Report(*pages)

对于您的情况,我认为您可以执行以下操作

  1. 创建一个列表
    • rows=[]
  2. 将行添加到列表
    • rows.append(row_1)
    • rows.append(row_2)
  3. 然后创建报告
    • r= dp.Report(*rows)

我在 datapane's GitHub and in this notebook 的最后一行代码中找到了这个解决方案。

这就是我解决这个问题的方法。

channel_graph_list=[]
for i in range(0,len(unique_channels),1):
    channel_1_name = unique_channels[i]
    filtered_data = filter_the_data(source=channel_data,filter_1='channel',fv_1=channel_1_name)
    get_chart = plot_chart(filtered_data,Y_axis_1='sessions:Q',Y_axis_2='conversion:Q',chart_title='Session & Conv. Chart for '+channel_1_name)
    
    #This is where the trick starts - The below code creates a dynamic variable
    vars() ["channel_row_"+str(i)] = get_chart
     
    channel_graph_list.append("dp.Plot(channel_row_"+str(i)+",label='"+channel_1_name+"')")
    

#convert the list to a string
channel_graph_row = ','.join(channel_graph_list)

# assign the code you want to run
code="""channel_graph = dp.Select(blocks=["""+channel_graph_row+ """],type=dp.SelectType.TABS)"""
#execute the code
exec(code)

希望上述解决方案能帮助其他希望将动态生成的参数传递给任何函数的人。