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



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

我使用Altair进行绘图;数据窗格";要创建报告,可以在此处找到相同的文档: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)

问题:当我知道有多少频道,但我的频道列表是动态的时,这很好。我喜欢用";对于";循环生成行,但不确定如何在dp中将这些行作为kwargs传递。Report((函数。例如,如果我有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的GitHub和这个笔记本的最后一行代码中找到了这个解决方案。

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

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)

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

最新更新