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

709 Views Asked by At

I am working on a charting module where I can pass on dataframe and the module will create reports based on plots generated by calling few functions as mentioned below.

I am using Altair for plotting and "Datapane" for creating the report, the documentation of the same can be found here : https://datapane.github.io/datapane/

My DataFrame looks like this

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)

Plotting functions :

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

The above functions when called, will filter the dataframe, create plot for each filters and group 2 plots in a row.

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'))

I can now generate a report by calling datapane.Report() function as follows

r= dp.Report(row_1,row_2)

Problem: This works fine when I know how many channels are present, but my channel list is dynamic.I am thing of using "for" loop to generate rows, but not sure how can I pass on these rows as kwargs in dp.Report() function. For example, if I have 10 channels, I need to pass 10 rows dynamically.

2

There are 2 best solutions below

1
mjd On BEST ANSWER

I had a similar problem and solved it as follows

  1. Create a list to store the pages or elements of the report, such as
    • report_pages=[]
    • report_pages.append(dp.Page)
    • report_pages.append(dp.Table)
    • report_pages.append(dp.Plot)
  2. At the end just generate the report with a pointer to the list
    • dp.Report(*pages)

In your case, I think you can do the following

  1. create a list
    • rows=[]
  2. add the rows to the list
    • rows.append(row_1)
    • rows.append(row_2)
  3. and then create the report with
    • r= dp.Report(*rows)

I found this solution on datapane's GitHub and in this notebook in the last line of code.

0
shreekant das On

So here is how I solved this problem.

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)

Hope the above solution helps others looking to pass dynamically generated parameters into any function.