# Allows print statements with variable background and font color and font size
bgcolor = 'cyan'; text_color = 'purple';
def div_print(text, width='auto', bgcolor=bgcolor, text_color=text_color,
fontsize=2):
from IPython.display import HTML as html_print
return display(html_print("<span style = 'display: block; width: {}; \
line-height: 2; background: {};\
margin-left: auto; margin-right: auto;\
border: 1px solid text_color;\
border-radius: 3px; text-align: center;\
padding: 3px 8px 3px 8px;'>\
<font size={}><text style=color:{}>{}\
</text></font></style>".format(width, bgcolor,
fontsize,
text_color, text)))
parse_links() creates 3 lists from the given list of topics to be included in the table of contents and navigation menu. These will be used by table_of_contents()
and link_menu()
to create the markdown code.
def parse_links(basic_list):
link_names = []
link_links = []
link_tags = []
for item in basic_list:
item_parts = item.split()
name = "[" + item.title() + "]"
link_names.append(name)
try:
item_tag = "<a name = '" + item_parts[0] + "_" + item_parts[1] + "'></a>"
item_link = "(#" + item_parts[0] + "_" + item_parts[1] + ")"
except:
item_tag = "<a name = '" + item_parts[0] + "'></a>"
item_link = "(#" + item_parts[0] + ")"
link_links.append(item_link.lower())
link_tags.append(item_tag.lower())
return link_names, link_links, link_tags
table_of_contents() produces and prints all the markdown code need, which can be cut and pasted into a markdown cell for the table of contents. It prints to make your table of contents. It also returns the list of corresponding tags to be embedded in the cells the links should jump to.
This is where parse_links()
comes in. table_of_contents()
calls it on the two column lists and creates the bits of code that are then assembled into the long string of markdown code that becomes a table. I have currently created this function to make a table of contents with two columns that have their own
def table_of_contents(title='', head1 = None, head2=None, col1=[], col2=[]):
from IPython.display import HTML
table_data = "<center><font size=6> " + title + "</font></center> \n \n|<font size=4> " + head1 + "| |<font size=4> " + head2 + "</font>| | \n|---|---|---|---| \n"
divider = "| | "
col1_names, col1_links, col1_htmls = parse_links(col1)
col2_names, col2_links, col2_htmls = parse_links(col2)
html_list = col1_htmls + col2_htmls
count = 0
while count < max(len(col1), len(col2)):
table_data = table_data + divider
try:
data1 = col1_names[count] + col1_links[count]
table_data = table_data + data1
except:
table_data = table_data
try:
data2 = col2_names[count] + col2_links[count]
table_data = table_data + divider + data2 + "| \n"
except:
table_data = table_data + divider + "| \n"
count +=1
table_data = "--- \n" + table_data + " \n---"
print(""); print(table_data)
print("")
link_string = str(html_list)
link_string = link_string.replace("[", "")
link_string = link_string.replace("]", "")
link_string = link_string.replace(',', " | ")
link_string = link_string.replace('"', "")
return(link_string)
Now when I call table_of_contents()
and pass to it the title of my table of contents, the headings for the both columns, and the lists of topics to be included in each column, it will print the markdown code, which can be immediately pasted into a markdown cell, resulting in a nice table of contents. It also returns the tags that must be embedded in the cells the table of contents links to. And there you have it! A lovely table of contents!
print(""); div_print("↙ CUT AND PASTE THE MARKDOWN CODE BELOW INTO A MARKDOWN CELL ↙", fontsize=4)
links_list = table_of_contents(title='Table of Contents',
head1 = "Heading One", head2="Heading Two",
col1=['Topic One', 'Topic Two', 'Topic Three'],
col2=['Topic One', 'Topic Two', 'Topic Three'])
--- <center><font size=6> Table of Contents</font></center> |<font size=4> Heading One| |<font size=4> Heading Two</font>| | |---|---|---|---| | | [Topic One](#topic_one)| | [Topic One](#topic_one)| | | [Topic Two](#topic_two)| | [Topic Two](#topic_two)| | | [Topic Three](#topic_three)| | [Topic Three](#topic_three)| ---
Heading One | Heading Two | ||
---|---|---|---|
Topic One | Topic One | ||
Topic Two | Topic Two | ||
Topic Three | Topic Three |
The code below is returned by table_of_contents()
. It contains all the tags to be embedded in their respective cells. Each corresponds to a link in the table of contents above.
links_list
"<a name = 'topic_one'></a> | <a name = 'topic_two'></a> | <a name = 'topic_three'></a> | <a name = 'topic_one'></a> | <a name = 'topic_two'></a> | <a name = 'topic_three'></a>"
link_menu() takes a list of topics and creates a navigation link menu and corresponding tags to embed in the appropriate cells for navigation.
def link_menu(links_list, title=None, links_per_row=5):
count = len(links_list)
current = 0
link_names, link_links, link_tags = parse_links(links_list)
link_string = "--- \n<font size=5> " + title + "</font> \n"
tags_data = ""
for link_item in range(count):
if current > count:
break
if (current + 1) % links_per_row == 0:
link_string = link_string + " | " + link_names[current] + link_links[current] + " | "
link_string = link_string + " \n"
else:
link_string = link_string + " | " + link_names[current] + link_links[current]
tags_data = tags_data + " | " + link_tags[current]
current += 1
link_string = link_string + " \n---"
print(link_string)
print("")
return tags_data
Pass to link_menu()
the a list of topics for which it should create a navigation menu and corresponding tags for embedding.
print(""); div_print("Cut and paste all of the following code into a markdown cell for a happy little navigation menu.", fontsize=4)
tags = link_menu(['Topic One', 'Topic Two', 'Topic Three', 'Topic Four', 'Topic Five',
'Topic Six', 'Topic Seven','Topic Eight','Topic Nine','Topic Ten',
'Topic Eleven','Topic Twelve'],
title = "Notebook Topics",
links_per_row = 6)
--- <font size=5> Notebook Topics</font> | [Topic One](#topic_one) | [Topic Two](#topic_two) | [Topic Three](#topic_three) | [Topic Four](#topic_four) | [Topic Five](#topic_five) | [Topic Six](#topic_six) | | [Topic Seven](#topic_seven) | [Topic Eight](#topic_eight) | [Topic Nine](#topic_nine) | [Topic Ten](#topic_ten) | [Topic Eleven](#topic_eleven) | [Topic Twelve](#topic_twelve) | ---
Notebook Topics
| Topic One | Topic Two | Topic Three | Topic Four | Topic Five | Topic Six |
| Topic Seven | Topic Eight | Topic Nine | Topic Ten | Topic Eleven | Topic Twelve |
The code below contains all the tags returned by link_menu() to be embedded in their respective cells.
tags
" | <a name = 'topic_one'></a> | <a name = 'topic_two'></a> | <a name = 'topic_three'></a> | <a name = 'topic_four'></a> | <a name = 'topic_five'></a> | <a name = 'topic_six'></a> | <a name = 'topic_seven'></a> | <a name = 'topic_eight'></a> | <a name = 'topic_nine'></a> | <a name = 'topic_ten'></a> | <a name = 'topic_eleven'></a> | <a name = 'topic_twelve'></a>"