Jupyter2Hashnode: An Effortless Way to Convert Jupyter Notebooks to Hashnode Stories

Jupyter2Hashnode: An Effortless Way to Convert Jupyter Notebooks to Hashnode Stories

This story was created using this notebook.

Introduction

As a data scientist, you want to share your work with others and reach a larger audience. Medium is a popular platform for this purpose, but it has limitations when it comes to data representation. To overcome this, I discovered Hashnode, a platform specifically designed for developers and the tech industry. It offers a powerful editor that supports markdown text, including tables, and provides an Amazon AWS S3 bucket for uploading images.

To simplify the process of converting Jupyter Notebooks into Hashnode stories, I created a tool called Jupyter2Hashnode. With Jupyter2Hashnode, you can convert Jupyter Notebooks into Hashnode stories with just a single command. The tool compresses images, uploads them to the Hashnode server, updates image URLs in the markdown file, and finally, publishes the story article.

Getting Started with Jupyter2Hashnode

Create a Python environment and install the Jupyter2Hashnode package.

Install jupyter2hashnode with pip:

PyPI version

    $ pip install jupyter2hashnode

Obtain the JWT token, Hashnode API token, and Publication ID.

Set the environment variables or pass the JWT token, Hashnode API token, and Publication ID when calling the Jupyter2Hashnode command.

See the full documentation.

Using Jupyter2Hashnode as a Command Line Tool

Jupyter2Hashnode converts the specified Jupyter Notebook to a Hashnode publication story, compresses images, uploads images to the Hashnode server, and replaces image URLs in the markdown file, then published. If the JWT token, API token, and Publication ID are not passed as arguments, the tool will use the environment variables HASHNODE_JWT, HASHNODE_TOKEN, and HASHNODE_PUBLICATION_ID.

Usage:

    $ python3 -m jupyter2hashnode [OPTIONS] NOTEBOOK_PATH [OUTPUT_PATH]

Arguments:

  • NOTEBOOK_PATH: notebook file name or complete path [required]

  • [OUTPUT_PATH]: output folder name or complete output path where the files will be written to if none creates an output folder with the same name as the notebook file name

Options:

  • -j, --jwt TEXT: JWT token for authentication at hashnode.com/api/upload-image.

  • -t, --token TEXT: Token for authentication at api.hashnode.com mutation createPublicationStory endpoint

  • -p, --publication TEXT: ID of the Hashnode publication e.g. hashnode.com//dashboard

  • --title TEXT: Article title [required]

  • --hide-from-feed / --no-hide-from-feed: Hide this article from Hashnode and display it only on your blog [default: True]

  • --delete-files / --no-delete-files: Delete all files after creating the publication story [default: True]

  • --upload / --no-upload: Upload the publication story to the Hashnode server [default: True]

  • -v, --version: Show the application's version and exit.

  • --install-completion: Install completion for the current shell.

  • --show-completion: Show completion for the current shell, to copy it or customize the installation.

  • --help: Show this message and exit.

Creating and Publishing a Jupyter Notebook to Hashnode

Setting up the environment

To work on your project, you first need to create a project folder and a virtual environment within it:

macOS/Linux

    $ mkdir myproject
    $ cd myproject
    $ python3 -m venv venv

Windows

    > mkdir myproject
    > cd myproject
    > py -3 -m venv venv

Next, activate the environment:

macOS/Linux

    $ . venv/bin/activate

Windows

    > venv\Scripts\activate

Install jupyter2hashnode with pip:

PyPI version

    $ pip install jupyter2hashnode

Environment variables

JWT To obtain JWT:

  1. Open hashnode.com, your account must be logged in

  2. Open DevTools of chrome browser (F12)

  3. Go to the Application tab, go to Cookies, and find and copy the value of "jwt" cookie (245 characters)

API TOKEN To obtain Hashnode API token:

  1. Open hashnode.com/settings/developer

  2. Click on "Generate New Token" button or use the existing one

PUBLICATION ID To obtain Publication ID:

  1. Go to hashnode.com/settings/blogs

  2. Click "Dashboard" of the blog you want to upload to

  3. Copy the ID, e.g. hashnode.com<id>/dashboard

Setting the environment variable:

  • macOS/Linux:
    $ export HASHNODE_JWT=<your_jwt>
    $ export HASHNODE_TOKEN=<your_token>
    $ export HASHNODE_PUBLICATION_ID=<your_id>
  • Windows command line:
    > set HASHNODE_JWT=<your_jwt>
    > set HASHNODE_TOKEN=<your_token>
    > set HASHNODE_PUBLICATION_ID=<your_id>
  • Windows Powershell:
    > $env:HASHNODE_JWT="<your_jwt>"
    > $env:HASHNODE_TOKEN="<your_token>"
    > $env:HASHNODE_PUBLICATION_ID="<your_id>"

Exporting and Publishing a Notebook

Note: You can check the provided notebook examples in your virtual environment .venv\Lib\site-packages\jupyter2hashnode\examples

If you have set environment variables, publish the story of example.ipynb, run:

$ python3 -m jupyter2hashnode example.ipynb

You will be prompted to provide a title for your article.

If you have not set environment variables, you can pass the required options in the command, such as the token:

$ python3 -m jupyter2hashnode example.ipynb --token "<your_token>"

The process of publishing a Jupyter Notebook with jupyter2hashnode involves the following steps:

  • Exporting the Jupyter notebook file example.ipynb into a markdown file.

  • Compressing and uploading all images in the markdown file to the Hashnode s3 amazon AWS bucket, generating corresponding URLs for the images.

  • Replacing the local paths of the images in the markdown file with the generated URLs.

  • Uploading the markdown text to Hashnode to create a new article.

The output of the above command should look something like this:

    Starting the publication...
    Creating markdown file at md_example ...
    Compressing and uploading images from  md_example ...
    Story publication from file md_example\example.md  with the title 'My published example'...
    Result [200]: Publication article created successfully
    Hashnode Post URL: https://hashnode.com/edit/clduiv6op0174d...

Viewing the Published Article

You can access your newly published article by clicking on the URL provided in the output or by checking your Hashnode dashboard.

To make the article visible to others and show up in Hashnode's feed, edit the article, uncheck the option Yes, hide this article from Hashnode and display it only on my blog, and click update.

Conclusion

Hashnode is a hassle-free blogging and community platform for developers and the tech industry. Jupyter2Hashnode is a valuable tool for converting Jupyter Notebooks into Hashnode stories, making it effortless to share your work with others. Try Jupyter2Hashnode today and experience the convenience of publishing your Jupyter Notebooks on Hashnode.