user525966
user525966

Reputation: 499

How to hyperlink in a Jupyter notebook?

In my Jupyter notebook I want the ability to have clickable links that either take you to specific sections of the same notebook or specific sections of another notebook. Is this possible?

Upvotes: 33

Views: 45567

Answers (3)

Jorge
Jorge

Reputation: 46

With Python, there is a better way to create a hyperlink for a table of content and avoid having to do-dashes-in-all-the-words manually, which would be to use input for the text, save it to a variable, use the replace() function, and the .format method.

So basically, you enter the hyperlink's title, then the link is copied to the clipboard to paste it in the markdown cell where you are writing your menu of content. It will save you a few clicks and time. For this to work, it is necessary to install and import the pyperclip library to be able to save the hyperlink to the clipboard:

To make hyperlinks that take you to another section on the same jupyter notebook:

import pyperclip as pc
link_title = input('Enter hyperlink title:')
dashed_title = link_title.replace(" ", "-")
link = ('[{}](#{})'.format(link_title, dashed_title))
pc.copy(link)

Example result copied to the clipboard:

[Section 1](#Section-1)

enter image description here

As a function:

def link_menu(hyperlink_title):
    import pyperclip as pc    
    dashed_title = hyperlink_title.replace(" ", "-")
    link = ('[{}](#{})'.format(hyperlink_title, dashed_title))
    return pc.copy(link)

Calling the function, passing as an argument the string you want as the hyperlink:

link_menu('Section 1')

To make hyperlinks that take you to another section of a different Jupyter notebook:

import pyperclip as pc
link_title = input('Enter hyperlink title:')
external_notebook_name = input('Enter external notebook name:')
dashed_title = link_title.replace(" ", "-")
link = ('[{}]({}.ipynb#{})'.format(link_title, external_notebook_name, dashed_title))
pc.copy(link)

Example result copied to the clipboard:

[Section 9](another_notebook.ipynb#Section-9)

As a function:

def link_menu_other(hyperlink_title, name_external_notebook):
    import pyperclip as pc
    dashed_title = hyperlink_title.replace(" ", "-")
    link = ('[{}]({}.ipynb#{})'.format(hyperlink_title, name_external_notebook, dashed_title))
    return pc.copy(link)

Calling the function, passing two strings as arguments, the first for the name of the hyperlink, the second for the name of the external jupyter notebook

link_menu_other('Section 10 to another notebook', 'other_notebook')

Upvotes: 1

CrepeGoat
CrepeGoat

Reputation: 2515

For anyone using Google Colaboratory:

Linking within file:

  • click on the cell you want to link to
  • in the top right, click the ellipses (...), and click Link to cell
  • copy only the suffix of the hyperlink, starting with the hashtag #
  • create a link in your markdown with the hashtag suffix (e.g., [Section 5](#hashtag_suffix))

Linking to other files

similar to above; instead copy the full cell link

Upvotes: 5

impopularGuy
impopularGuy

Reputation: 867

To create a internal clickable link in the same notebook:

Step 1: Create link

[To some Internal Section](#section_id)

Step 2: Create destination

<a id='section_id'></a>

To create link in one notebook and destination in another notebook.

Step 1: Create link

[To Some Internal Section](another_notebook.ipynb#section_id2)

Step 2: Create Destination

<a id='section_id2'></a>

If the notebook is inside a folder present in the current working directory:

[To Some Internal Section](TestFolder/another_notebook.ipynb#section_id3)

Upvotes: 47

Related Questions