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?
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.
You can insert cross-references to labels in your content with two kinds of syntax: {ref}`label-text` [](label-text)
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)
For anyone using Google Colaboratory:
Link to cell
#
[Section 5](#hashtag_suffix)
)similar to above; instead copy the full cell link
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:
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)
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')
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With