Skip to content

Instantly share code, notes, and snippets.

@jamescalam
Last active May 5, 2020 19:36
Show Gist options
  • Select an option

  • Save jamescalam/8a46c23d8b7fb23ef46edd4a0b740269 to your computer and use it in GitHub Desktop.

Select an option

Save jamescalam/8a46c23d8b7fb23ef46edd4a0b740269 to your computer and use it in GitHub Desktop.
Dynamic breadcrumb navbar with Python.
# initialise the breadcrumb and add link to top-level readme
html += f"""
<!-- Breadcrumb Navigation -->
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item active"><a href="../readme.html">Readme</a></li>
"""
# loop through each layer in full path and add each to breadcrumb
for i, layer in enumerate(fullpath):
# check if we are on the final item
if i+1 != len(fullpath):
path = fullpath[:i+1] # get all items leading to this point
# format each part to not contain capitals or spaces for file links
path = [part.lower().replace(" ", "_") for part in path]
path = ".".join(path) + ".html" # create the filename
# add in a hypterlink
html += f"""
<li class="breadcrumb-item active"><a href="{path}">{layer}</a></li>
"""
else:
# otherwise, no hyperlink as is current page
html += f"""
<li class="breadcrumb-item" aria-current="page">{layer}</li>
"""
# add ending tags of navbar section
html += """
</ol>
</nav>
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment