Screenshot 2020-08-18 at 09.44.32.png

Guide

How to Embed Python Analyses into Notion, Confluence, and more.

Leo Anthias

Leo Anthias

If you've generated visualizations and datasets in a Python and want to share them with your colleagues or community, what is the best process?

Motivation

If you're sharing an analysis at work, people are probably already using a platform like Confluence, Notion, or an internal knowledge base. If you're sharing with your community, they probably already read content on Medium, Reddit, or your blog.

It's often important that your stakeholders and readers aren't forced to learn a whole new workflow or tool, so data scientists often take take screenshots of datasets and plots and copy them into these existing platforms.

This presents two problems.

  1. Readers find plots and data more impressive and understandable when they can interact and explore them themselves, and they are more engaged when they can touch and feel your data and visualizations. You lose a lot by sending a static screenshot of that beautiful plot you just built.
  2. Once you embed that image — it can't change, unless you go in and manually upload a new one. What if you wanted to embed a forecast into Salesforce which updates automatically over time? What if you want to share a cohort analysis with a PM which takes the numbers from the last week? An image can be good for showing a single snapshot in time, but it makes dynamic use-cases impossible, so data scientists build custom reports and send new results back and forth over email.

Solution

Wouldn't it be amazing if you could turn the existing platforms you use into interactive dashboards for your Python analyses, and embed visualisations from Plotly, Altair, Matplotlib, and Folium?

This is one of the problems which Datapane's API solves. Datapane is an open-source framework for people who analyze data in Python and need a way to share their results.

Previously we learned how Datapane helps you create an interactive report, share your analytics results as a standalone website, and share results with your co-workers. These use-cases demonstrated how to create a standalone report which lives on a URL.

One of the benefits of Datapane is that, once you have created a report using the API, you can embed the whole report (or a single result) into the different platforms which you already use, such as:

  • Notion
  • Confluence
  • Coda
  • Reddit
  • Medium
  • Salesforce
  • Your own webpage

This means that it takes seconds to share your analysis with your team or community through the tools you are use. Additionally, because Datapane reports can be updated and generated dynamically, you can make sure that your report is always up-to-date.

In this guide, we will explore how to embed your charts and datasets in each of these platforms using Datapane.

How to Embed Datapane Reports Across Platforms

To create a Datapane report, you just need to add dp.Report(dp.Plot(your_plot)) at the end of your existing Jupyter Notebook or Python script. You don't need to write your code in a special way; instead, just import datapane and tell it which plots and datasets you want in your report.

  1. !pip3 install datapane
  2. # Get your secret token after logging in and insert here
  3. !datapane login --token=[your_secret_token]
  4. import pandas as pd
  5. import altair as alt
  6. import datapane as dp
  7. df = pd.read_csv('https://query1.finance.yahoo.com/v7/finance/download/GOOG?period2=1585222905&interval=1mo&events=history')
  8. chart = alt.Chart(df).encode(
  9. x='Date:T',
  10. y='Open'
  11. ).mark_line().interactive()
  12. r = dp.Report(dp.Table(df), dp.Plot(chart))
  13. r.publish(name='stock_report', open=True)

For this tutorial, we have created a sample notebook which you can customize and run to generate your own report on Datapane.

This script takes a search query (try picking your favorite artist, food, sport, or hobby!) and finds related queries and plots how popular it is in different countries using Plotly and Folium.

Run the notebook yourself on Binder to see how to create the report below, or download it from GitHub.

Medium

Medium is an online publishing platform. To embed your report on Medium, copy the link into your Medium post in Edit Mode.

Voila! — an embedded chart is automatically created for you inside your post. You will not be able to interact with the chart yet in the Edit mode, but you will see the interactive chart after your post is published.

Screenshot 2020-08-17 at 19.05.58.png

Notion

Notion is a great tool which combines your everyday work apps into one, including notes, to-do lists, resources, images, and tables. If you are already using Notion in your company, and you're comfortable analyzing data using Python, you might not need to buy a clunky BI tool. Instead, you can generate interactive, dynamic reports in Python and embed them straight into your Notion documents.

To embed the chart in Notion, just paste the link into any page. A menu will pop up asking you Create Embed. If you click this, Notion will embed your report inside your page.

Check out this sample dashboard created using Notion and Datapane!

Your website

You can embed into your own website using an iframe. After publishing your report, if you browse to it's page on Datapane.com, and select Embed in the share menu, you will be able to copy and paste a snippet of code to embed it into your page.

Screenshot 2020-08-17 at 19.34.11.png

You can use this on any website.

Confluence

You can embed a Datapane report into Confluence using an iframe, but instead of copying the code tag like you do when embedding in your own website, you need to use Confluence's built-in iframe support.

On your Confluence page, type /iframe and the iframe popup will appear.

Screenshot 2020-08-17 at 19.05.58.png

Similarly to when you are embedding in your own website, when you embed into Confluence, make sure you add /embed to the end of your URL, as Confluence does not automatically add it as Notion, Medium, and Reddit do.

Confluence will then display your report or visualization in the page:

Screenshot 2020-08-17 at 19.11.47.png

Reddit

To embed your report into Reddit, you just need to copy and paste it's URL into any subreddit which supports rich embedding.

This means you can paste your interactive plots to subreddit like /r/dataisbeautiful, and have them interactive in the browser, such as this Altair visualization: https://old.reddit.com/r/dataisbeautiful/comments/hadl1g/oc_foreignborn_population_percentage_per_county/

Screenshot 2020-08-18 at 09.55.19.png

How to Share Specific Components

Instead of sharing the entire report like above, you may only want to share a specific component like the plots within the report and excluding the text:

When you create a report on Datapane, it is turned into an document which you can query using XPath. This provides a flexible way to extract the components you need from your report.

Let’s say there are both markdown and plot in your report, you may want to share the plots in your report. Simply add /?blocksquery=//Plot at the end of the report link.‌

For example, to extract the plots in this report: https://datapane.com/khuyentran1401/reports/trend_analysis_75b01eba, we add /?blocksquery=//Plotat the end of the link like this: https://datapane.com/khuyentran1401/reports/trend_analysis_75b01eba/?blocksquery=//Plot. Now we will just see the plot components.

What other components that I can specify besides Plot?

There are 4 component types in Datapane you can specify in the link

  • Table : Extract dataframe
  • Plot : Extract plots
  • Text : Extract markdown
  • File : Extract files or images

Simply replace Plot in this link: https://datapane.com/khuyentran1401/reports/trend_analysis_75b01eba/?blocksquery=//Plot

with Table :

https://datapane.com/khuyentran1401/reports/trend_analysis_75b01eba/?blocksquery=//Table

Now you will just see the table. If you want to include only the second plot of the report, simply add /?blocksquery=//Plot[2] at the end of the link, which will give you the following:

Need to share Python analyses?

Datapane is an API and framework which makes it easy for people analysing data in Python to publish interactive reports and deploy their analyses.