Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 229
user525966 Avatar asked Mar 28 '18 13:03

user525966


People also ask

How do you make a header in Jupyter Notebook?

h1, h2, h3, h4, h5 and h6. h1 is the largest, h6 is the smallest. To write a header, include # before the text for h1 header, ## before the text for h2 header, ### before the text for h3 header, and so on. NOTE: In Jupyter notebooks, you must leave a space between the group of # signs and the text.

How do you cross reference in a Jupyter Notebook?

You can insert cross-references to labels in your content with two kinds of syntax: {ref}`label-text` [](label-text)


3 Answers

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)
like image 62
impopularGuy Avatar answered Oct 19 '22 06:10

impopularGuy


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

like image 33
CrepeGoat Avatar answered Oct 19 '22 05:10

CrepeGoat


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')
like image 1
Jorge C Avatar answered Oct 19 '22 04:10

Jorge C