Publish Data Science Articles to the Web using Jupyter, Github and KysoCombine these 3 tools to supercharge your DS workflowKyleBlockedUnblockFollowFollowingMay 6Data science is exploding, more and more organizations are using data to power, well, everything.
But it can sometimes still be a little difficult to publish data-science based reports.
This might be because the charts are interactive, or because you want a reproducible document, or because it can be difficult to move from the data-science tools to more readable formats.
In this article I’ll go through how using python in Jupyter notebooks, Github and Kyso we can go from getting some raw data to publishing an awesome chart to the web in a few minutes.
Let’s start analyzing some dataI start with assuming you have python3 installed — otherwise go to https://www.
python.
org/ and download it.
Then let’s install Jupyterlab and the plotly extension for awesome interactive charts.
pip3 install jupyterlabjupyter labextension install @jupyterlab/plotly-extensionThen let’s start Jupyterlab withjupyter labAnd let’s download some data to play with, I’ve used the recent Eurobarometer data to see how many people in the EU identify with the EU flag, available here.
So now you should have Jupyter open — its a nice interactive place where you can type results and see the live output.
We can then start writing code in the first notebook cell!import plotlyimport pandas as pdimport plotly.
graph_objs as goimport country_converter as cocoplotly.
offline.
init_notebook_mode(connected=True)This will import all the libraries we need and setup Plotly up to render the chart inside the Jupyter notebook.
cols2skip = [0]cols = [i for i in range(100) if i not in cols2skip]df_raw = pd.
read_excel('.
/eb_90_volume_A.
xls', sheet_name="QD11.
3", skiprows=8, usecols=cols)df_raw.
rename(columns={'UE28.EU28':'EU28', 'UE28-UK.EU28-UK':'EU28-UK' }, inplace=True)df = df_raw.
transpose()df = df.
rename(columns=df.
iloc[0])df = df.
iloc[1:]names = df.
index.
tolist()names = ['GR' if (x == 'EL') else x for x in names]names = ['GB' if (x == 'UK') else x for x in names]iso3 = coco.
convert(names=names, to='ISO3', not_found=None)Then we can wrangle with the data and set it up for plotting, there’s not much to explain here, I came up with this code after a few minutes of playing around and examining the data-shape.
Finally, let’s plot the data:data = [go.
Choropleth( locations = iso3, z = df['Tend to agree'] * 100, text = iso3, locationmode="ISO-3", reversescale=True, colorscale="Blues", marker = go.
choropleth.
Marker( line = go.
choropleth.
marker.
Line( color = 'rgb(0,0,0)', width = 0.
5 )), colorbar = go.
choropleth.
ColorBar( ticksuffix = '%', title = '% identify', len=0.
5, ),)]layout = { "height": 700, "width": 700, "margin" : {"t": 0, "b": 0, "l": 0, "r": 0}, "geo": { "lataxis": {"range": [36.
0, 65.
0]}, "lonaxis": {"range": [-12.
0, 36.
0]}, "projection": {"type": "transverse mercator"}, "resolution": 50, "showcoastlines": True, "showframe": True, "showcountries": True, }}fig = go.
Figure(data = data, layout = layout)plotly.
offline.
iplot(fig)You should now have a nice interactive chart inside your notebook.
Plotly Chart in JupyterlabIn Jupyter you can also write a bunch of annotations to your analysis by changing the cell to a markdown type, and then writing whatever you like.
Let’s push this notebook to the webWe have a few choices now to get this notebook on the web where we can share it.
I’ve already pushed this to the web at http://kyso.
io/eoin/do-i-identify-with-eu-flag where you can see a preview.
Our first choice is that we can just upload it to Kyso.
Go to https://kyso.
io/create/studyand just drop in the .
ipynb file, give it a title and you will have a link to share.
Or else we can use Github for a longer-term project where we might be making a lot of changes.
To start head over to Github and make a new project, then do the normal business ofgit add .
git commit -m "initial commit"git push origin masterYou will then be able to view your project, and notebook on Github.
However, the Plotly plot won’t be visible.
So let’s import this project to Kyso.
Head to https://kyso.
io/github and sign in with Github, search for your project, hit “Connect to Kyso” for that project, select the main file and you’re done.
Your notebook will be visible on Kyso and any time you commit to that repository it will get reflected on Kyso meaning you can continuously update your analysis.
For example, check out this post on Kyso, which is linked to this Github repository.
.