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?
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.
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:
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.
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.
- !pip3 install datapane
- # Get your secret token after logging in and insert here
- !datapane login --token=[your_secret_token]
- import pandas as pd
- import altair as alt
- import datapane as dp
- df = pd.read_csv('https://query1.finance.yahoo.com/v7/finance/download/GOOG?period2=1585222905&interval=1mo&events=history')
- chart = alt.Chart(df).encode(
- r = dp.Report(dp.Table(df), dp.Plot(chart))
- 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.
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.
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!
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.
You can use this on any website.
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.
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:
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/
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.
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
Plot in this link:
Now you will just see the table. If you want to include only the second plot of the report, simply add
/?blocksquery=//Plot at the end of the link, which will give you the following: