Usage

Setup

Create a Python environment and install jupyter2hashnode package, check the instructions here.

Then is necessary to get the JWT token, Hashnode Api Token and the Publication ID.

JWT To obtain JWT:

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

  2. Open DevTools of chrome browser (F12)

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

Setting the environment variable:

  • macOS/Linux:

$ export HASHNODE_JWT=<your_jwt>
  • Windows command line:

> set HASHNODE_JWT=<your_jwt>
  • Windows Powershell:

> $env:HASHNODE_JWT="<your_jwt>"

1c2f56869baf4f38aa8f9d463f4a2afe

API TOKEN To obtain Hashnode API token:

  1. Open https://hashnode.com/settings/developer

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

Setting the environment variable:

  • macOS/Linux:

$ export HASHNODE_TOKEN=<your_token>
  • Windows command line:

> set HASHNODE_TOKEN=<your_token>
  • Windows Powershell:

> $env:HASHNODE_TOKEN="<your_token>"

a4385432252e4acf91610ffd4ca4fb30

PUBLICATION ID To obtain Publication ID:

  1. Go to https://hashnode.com/settings/blogs

  2. Click “Dashboard” of the blog you want to upload to

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

Setting the environment variable:

  • macOS/Linux:

$ export HASHNODE_PUBLICATION_ID=<your_id>
  • Windows command line:

> set HASHNODE_PUBLICATION_ID=<your_id>
  • Windows Powershell:

> $env:HASHNODE_PUBLICATION_ID="<your_id>"

f23d892961484a0ca749bef53eb72b02

NOTE: We can set jwt, token and publication id values using environment variables or passing them when calling the jupyter2hashnode command.

As a command line tool

jupyter2hashnode converts the specified Jupyter Notebook to a Hashnode publication story, compressing images, uploading images to the Hashnode server, and replacing image URLs in the markdown file, then published.

If jwt, token, publication_id arguments not passed then will use environment variables HASHNODE_JWT, HASHNODE_TOKEN, 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 output folder with the same name as the notebook file name

Options:

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

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

  • -p, --publication TEXT: ID of the Hashnode publication e.g. https://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.

Help command:

$ python3 -m jupyter2hashnode --help

d499b45b2cd6406391d45595d3102feb

Version:

$ python3 -m jupyter2hashnode --version

As a library

class Jupyter2Hashnode

The Jupyter2Hashnode class is used to convert Jupyter Notebooks to Hashnode publication stories by compressing images, uploading images to the Hashnode server, and replacing image URLs in the markdown file.

Notes: - To obtain JWT

1. Open https://hashnode.com, account must be logged in

2. Open DevTools of chrome browser (F12)

3. Go to Application tab

4. Go to Cookies

5. Find and copy value of "jwt" cookie (245 characters)

Attributes:

HASHNODE_JWT (str): JWT token for authentication at Hashnode image uploader, https://hashnode.com/api/upload-image.
HASHNODE_TOKEN (str): Token for authentication with the Hashnode server, to use https://api.hashnode.com
                            mutation createPublicationStory endpoint
HASHNODE_PUBLICATION_ID (str): ID of the Hashnode publication e.g. https://hashnode.com/<id>/dashboard

Methods:

create_publication_story(title:str, notebook_path: str, output_path:Optional[str]=None,
                            delete_files:bool=True, upload:bool=True):
    This function is used to create a publication story on the Hashnode blog platform by
    converting a Jupyter Notebook to a markdown file, compressing images, uploading images
    to the Hashnode server, and replacing image URLs in the markdown file.

    Parameters:
        title (str): Title of the publication story.
        notebook_path (str): Path to the Jupyter Notebook file.
        hide_from_feed (bool): Hide this article from Hashnode and display it only on your blog, Default is True.
        output_path (str, optional): Path to the output directory. Default is None.
        delete_files (bool, optional): Boolean value indicating whether to delete all files after
                                        creating the publication story. Default is True.
        upload (bool, optional): Boolean value indicating whether to upload the publication story
                                    to the Hashnode server. Default is True.

    Returns:
        None

Usage:

from jupyter2hashnode import Jupyter2Hashnode

j2h = Jupyter2Hashnode(jwt, token, publication_id)
j2h.create_publication_story(title, notebook_path, hide_from_feed, output_path, delete_files, upload)

Example

When installing the package in a virtual environment some notebook examples are provided to test the story publication in the folder:

.venv\Lib\site-packages\jupyter2hashnode\examples

Let’s publish the story of example.ipynb:

$ python3 -m jupyter2hashnode .\.venv\Lib\site-packages\jupyter2hashnode\examples\example.ipynb

In this example we assume that environment variables have been created. If not then they could be passed as options in the command, check the “Using jupyter2hashnode as a command line tool” to view all options, for example passing the token:

$ python3 -m jupyter2hashnode .\.venv\Lib\site-packages\jupyter2hashnode\examples\example.ipynb --token "<your_token>"

When executing will ask for the title:

Article title: My published example

This will export the Jupyter notebook file notebook.ipynb into a markdown file and export all images, the images will be compressed and uploaded to the address https://hashnode.com/api/upload-image, Hashnode s3 amazon AWS bucket, that will generete the correspondent urls for the images. The local path of the images in the markdown file will be replaced by the urls generated and then all the markdown text will be uploaded into a Hashnode Article.

Execution output:

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...

We could click in url provided in the output or we can check the hashnode dashboard:

5ed56338bffd42aeb6d9a776f55a4509

To make the article visible to others making to show up in Hashnode’s feed, edit the article push Update and unchecked the option Yes, hide this article from Hashnode and display it only on my blog and click update again.

a3fd29e313c14da0886f63ff825117bf

Find more notebook examples here.