Publishing to Medium From Jupyter Notebooks | by Kurtis Pykes | Towards Data Science

Programmers, we all know the struggle of writing a notebook then having to copy and paste the code into Medium when we write a technical post... HEADACHE! To my delight, and probably yours, I’ve found the package that I believe is the saving grace for our programming on Medium woes.
Jupyter-to-Medium was created by a fellow Medium writer Ted Petrou. Ted channelled this frustration into what I believe is a very creative package — Click here to read more.
My motivations in this post are quite simple. I aim to demonstrate, as well as test the full capability of the Jupyter-to-Notebook package then add my thoughts on the package and advice for those that decide to adopt it as part of their blogging workflow.
Disclaimer: I am not a paid promoter and have not been asked in any way to write this story on Jupyter-to-Medium — I just thought it was a cool idea.
Those well versed with Jupyter Notebooks would know that LaTex is a built-in feature — and if you didn’t know, now you know. I constantly find myself having to search for images of mathematical expressions on google (that do not violate any copyright agreements), or in the worst case scenario I’d have to go on paint to design it myself before importing it into Medium as an image.
This process can be extremely laborious, talk less of how tedious it is. There have been numerous post I’ve had to delay publishing for up to an hour because I am creating images of my mathematical expressions — A disaster of an idea to not incorporate LaTex into Medium, but if there are justified reasons as to why it’s not (or if I just do not know how to do it) then I take that back.
As stated prior, LaTex is a feature of Jupyter Notebooks so I was hoping that when I implement LaTex within the notebook it would automatically be parsed as an image when imported into Medium. But —
Clearly this was not the case (above is an example of the LaTex code I wrote in my Jupyter Notebook).
Nonetheless, whenever I open a Jupyter Notebook environment it’s usually not for writing LaTex code in a markdown cell. Instead, I generally use the notebook to perform some type of prototyping, visualizations, analysis, etc. Ergo this property not meeting my initial expectation does not in anyway harm the prospects of this package.
A very annoying feat of writing technical stories on Medium is when you have to copy and paste all of your code that you have written in your Jupyter Notebook into Medium — thinking about it is getting me even more annoyed!
Jupyter-to-Medium translates the code cells we write in our notebook into a code block in Medium once we publish it across.
# example of code cell being translated to code block on mediumprint("Hello World")Hello World
Pretty cool right!
To further extend, when working in our Jupyter Notebook environment we often have some data that we are trying to explore. I’m not sure how others incorporate their DataFrames into their technical blogs, but I have to take a screenshot of each individual DataFrame and crop it so that I can upload it as an image in my Medium story — another process that takes unnecessarily long!
Having to screenshot, crop then save before uploading is also quite long and tedious. When the DataFrame has many columns the notebook adds an ellipses to the dataframe and scrollbar for any further columns that... I wanted to see how Jupyter-to-Medium handles this feat and to my joy, I was extremely impressed.
In Figure 1 you can see that the ellipses are present but the parser extracted the table and pasted it on Medium in a suitable size.
The demo data is from Kaggle Categorical Feature Engineering competition — Click here for access to the Dataset.
import pandas as pd df = pd.read_csv("../data/categorical_feature_engineering_raw/train.csv")df.head()
I often use Plotly for my visualizations, however when I initially tried to use it for this blog post my post resulted in a 60 minute long read for the exact same content I have in this current post. Upon further inspection, I realised that all my Plotly code had been converted into a long sequence of numbers (I also accidently published this to Towards Data Science — Sorry guys).
Long story short, I use Matplotlib for this overview.
The failure to incorporate Plotly may upset those that have made a complete switch from static plots to interactive, and those that didn’t even experience matplotlib. But, if you aren’t all that fussy about frameworks, you’ll be fine and matplotlib is pretty easy to learn!
Note: I didn’t test Seaborn, so if you do use it please let me know the result.
# https://matplotlib.org/api/pyplot_api.htmlimport numpy as npimport matplotlib.pyplot as pltx = np.arange(0, 5, 0.1);y = np.sin(x)plt.plot(x, y)plt.show()
# https://matplotlib.org/3.1.1/gallery/images_contours_and_fields/image_annotated_heatmap.htmlvegetables = ["cucumber", "tomato", "lettuce", "asparagus",              "potato", "wheat", "barley"]farmers = ["Farmer Joe", "Upland Bros.", "Smith Gardening",           "Agrifun", "Organiculture", "BioGoods Ltd.", "Cornylee Corp."]harvest = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],                    [2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],                    [1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],                    [0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],                    [0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],                    [1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],                    [0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])fig, ax = plt.subplots(figsize=(8, 10))im = ax.imshow(harvest)# We want to show all ticks...ax.set_xticks(np.arange(len(farmers)))ax.set_yticks(np.arange(len(vegetables)))# ... and label them with the respective list entriesax.set_xticklabels(farmers)ax.set_yticklabels(vegetables)# Rotate the tick labels and set their alignment.plt.setp(ax.get_xticklabels(), rotation=45, ha="right",         rotation_mode="anchor")# Loop over data dimensions and create text annotations.for i in range(len(vegetables)):    for j in range(len(farmers)):        text = ax.text(j, i, harvest[i, j],                       ha="center", va="center", color="w")ax.set_title("Harvest of local farmers (in tons/year)")fig.tight_layout()plt.show()
Those of you that follow my stories would know that I love to label my images. If you are wondering whether I labelled the Figures in Jupyter Notebook or Medium — the answer is “I done it on Medium”.
Last but not least, animations. I have never used animations before so I thought it may be cool to try it out.
# http://louistiao.me/posts/notebooks/embedding-matplotlib-animations-in-jupyter-as-interactive-javascript-widgets/from matplotlib import animationfrom IPython.display import Image%matplotlib inlinefig, ax = plt.subplots()ax.set_xlim(( 0, 2))ax.set_ylim((-2, 2))line, = ax.plot([], [], lw=2)def init():    line.set_data([], [])    return (line,)def animate(i):    x = np.linspace(0, 2, 1000)    y = np.sin(2 * np.pi * (x - 0.01 * i))    line.set_data(x, y)    return (line,)anim = animation.FuncAnimation(fig, animate, init_func=init,                               frames=100, interval=20,                                blit=True)anim.save('animation.gif', writer='imagemagick', fps=60)print("Static plot")Static plot
anim.save('animation.gif', writer='imagemagick', fps=60)Image(url='animation.gif')
Note: I did have to import the gif manually, but this may just be a result of my lack of familiarity of using gifs since in the main article Ted done there is a fully working gif there.
For full access to the code click on the link below, but beware that the initial upload from my Jupyter Notebook was my first rough draft hence you may notice significant changes from the markdown in the Notebook.
github.com
I think I have already made my opinion clear — I think the package is very creative and I would truly recommend that everyone that writes technical posts on Medium should avert to using this package. Despite this I would warn anyone that decides to adopt this package to beware that the purpose of Jupyter Notebooks and Medium contrast significantly and this should be taken into account.
Sharing on Medium is optimised to make writing more presentable and Jupyter Notebooks is for doing neat explorations. This becomes very apparent when we publish to Medium from our Notebooks since there are going to be quite a few tweaks to do for your Medium story to be presentable — at least in my opinion. I do not think publishing directly from Jupyter Notebooks is a good idea, you’d almost always want to edit the formatting and style (i.e. drop caps).
My best advice is to use both of platforms to their strengths thereby you should have an expectation to make adjustments from your initial Jupyter Notebook, but at the same time, it is important to appreciate that the majority of the heavy lifting has been done for you!
If you’d like to get in contact with me the best way is via LinkedIn