Skip to main content

Publish a Jupyter Notebook as a Medium blogpost

Project description

Jupyter to Medium

PyPI - License codecov

Publish Jupyter Notebooks as Medium blog posts directly from your notebook with the help of jupyter_to_medium.

Target User

Do you ....

  • Publish blog posts on Medium?
  • Use Jupyter Notebooks to write your posts?
  • Dislike the time and effort it takes to transfer your posts from Jupyter to Medium?
  • Get lost/bored when switching between the medium editor, gist etc to create well linted code?
  • Want to integrate LaTeX into your posts without manually screenshot-ing all your equation cells?

If so, jupyter_to_medium will automate the process of taking your Jupyter Notebook, as is, and publishing it as a Medium post in almost no time at all saving huge amounts of time.

Motivation

I've published dozens of blog posts on Medium myself with all of them beginning as Jupyter Notebooks. Manually converting them to Medium posts was a fairly lengthy, painstaking process. One particularly painful process was inserting tables, which Medium does not support, into my posts. Nearly all of my posts contain numerous pandas DataFrames (such as this one, which has 40! DataFrames) which are represented as HTML tables within a notebook. I'd take screenshots of each one to insert them into my Medium posts.

Installation

pip install jupyter_to_medium

Automatically activated

You should be able to skip the next step, but if the extension is not showing up in your notebook, run the following command:

jupyter bundlerextension enable --py jupyter_to_medium._bundler --sys-prefix

Get an Integration Token from Medium

Before using this package, you must request an integration token from Medium by emailing them at yourfriends@medium.com allowing you to create a token in your Medium settings. You can read the entire instructions on how to get your integration token.

Creating the integration token

Once your request to create integration tokens is accepted, navigate to your Medium settings. In the 'Secuity & Apps' section at the bottom you'll see 'Integration tokens'. Enter a description for the token (jupyter_to_medium is a good choice) and then create the token.

png

Save your integration token

Once you have your integration token, create the folder and file .jupyter_to_medium/integration_token in your home directory and save the token there. If you don't save it, you'll need to access it every time you wish to make a new post.

Create / save your github PAT (only required for gist integration)

When publishing, jtm can take unformatted code snippets and replace them with linted gists. In order to do this, it needs to create the gists which requires github access as well as a Personal Access Token (PAT). To create a github account, sign up here and then follow these instructions to create a PAT - ensure to select the option for creating gists.

Once you have your Github PAT, similar to the integration token, create the folder and file .jupyter_to_medium/github_token in your home directory and save the token there. If you don't save it, you'll need to access it every time you wish to make a new post.

Usage

There are three ways to publish notebooks:

  • Within an active notebook
  • From the command line
  • Using a Python script

Publishing to Medium within a Notebook

After installation, open the notebook you wish to publish and select the option File -> Deploy as -> Medium Post

png

A new browser tab will open with a short form that needs to be completed.

png

After clicking publish, the notebook and all images will be uploaded to Medium. Any HTML tables (such as pandas DataFrames) will be converted to images (via chrome), as Medium has no ability to represent tables. This is a time consuming process, so be prepared to wait. Check your terminal for updates. If successful, you'll get the following response with a link to view the post.

png

Click the link to view the post.

png

Finalize and publish on Medium

Currently, your post will be published as a draft. Review and publish the post on Medium.

Publishing to Medium from the Command Line

Upon installation, you'll have access to the command line program jupyter_to_medium with the same options as the below function.

jupyter_to_medium --pub-name="Dunder Data" --tags="python, data science" "My Awesome Blog Post.ipynb"

Publishing to Medium with a Python Script

In a separate script/notebook, import juptyer_to_medium as a module. Pass the publish function the location of the Jupyter Notebook you would like to publish as a Medium blog post.

import jupyter_to_medium as jtm
jtm.publish('My Awesome Jupyter Notebook.ipynb',
            integration_token=None,
            pub_name=None,
            title=None,
            tags=None,
            publish_status='draft',
            notify_followers=False,
            license='all-rights-reserved',
            canonical_url=None,
            chrome_path=None,
            save_markdown=False,
            table_conversion='chrome',
            gistify=False,
            gist_threshold=5,
            public_gists=True
            )

If successful, a message will be printed with the URL to your post. Additionally, JSON data will be returned as a dictionary containing the returned request from Medium.

Works for Classic Notebook not Jupyter Lab

Currently, this package only works for the "classic" Jupyter Notebook and is not available in Jupyter Lab. If you have experience making Jupyter Lab extensions, please let me know.

Troubleshooting

If your post is unsuccessful, a message with the error will be printed to the screen with information that might help you solve the issue.

Table conversion with Chrome or Matplotlib

By default, tables will be converted via Chrome web browser by taking screenshots of them. If you don't have Chrome installed or cannot get chrome to work, select 'matplotlib' for the table conversion.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jupyter_to_medium-0.2.12.tar.gz (29.8 kB view details)

Uploaded Source

Built Distribution

jupyter_to_medium-0.2.12-py3-none-any.whl (32.7 kB view details)

Uploaded Python 3

File details

Details for the file jupyter_to_medium-0.2.12.tar.gz.

File metadata

  • Download URL: jupyter_to_medium-0.2.12.tar.gz
  • Upload date:
  • Size: 29.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for jupyter_to_medium-0.2.12.tar.gz
Algorithm Hash digest
SHA256 6da33277019f56302b4682129ac44b4b9d8de7a602da05c4e9e13c9040d009b2
MD5 bd9042c2b2053a7c07d5e7bcfee3e566
BLAKE2b-256 af0856cc066fa367bc27b459eecd8a919f70642012124b46dcc2386f8fc3fb99

See more details on using hashes here.

File details

Details for the file jupyter_to_medium-0.2.12-py3-none-any.whl.

File metadata

File hashes

Hashes for jupyter_to_medium-0.2.12-py3-none-any.whl
Algorithm Hash digest
SHA256 3d5476888a55767ce1d8b9bd5ea1d9871f312afcdad9412388881484f0439389
MD5 7db65b86313da1277d397dec1508a192
BLAKE2b-256 b8dc9a7f83ed7dc74ae0a6cd2797155357168d3a4a2f91503a51e2b12d47b423

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page