Back to Python
2026-03-255 min read

Subnavigation Menu (Python Programming)

Learn Subnavigation Menu (Python Programming) step by step with clear examples and exercises.

Title: Subnavigation Menu (Python Programming)

Why This Matters

A subnavigation menu is an essential component in web development that provides a user-friendly interface for navigating through different sections of a website. In Python, we can create dynamic subnavigation menus using HTML, CSS, and JavaScript along with our Python scripts. This skill is crucial for building responsive and interactive websites, making it valuable for both beginners and experienced developers.

Prerequisites

Before diving into creating a subnavigation menu, you should have a basic understanding of:

  1. HTML: The structure of web pages, including tags like `, , and `.
  2. CSS: Styling and layout of web pages using properties like display, float, and margin.
  3. JavaScript: Basic understanding of JavaScript to handle user interactions and manipulate the DOM.
  4. Python Basics: Variables, functions, loops, and control structures.
  5. Flask: A micro web framework for Python that allows you to build web applications easily.
  6. JQuery: A popular JavaScript library that simplifies HTML document traversing, event handling, and animation.

Core Concept

To create a subnavigation menu in Python using Flask, we will follow these steps:

  1. Set up the project structure and install necessary dependencies.
  2. Create HTML templates for the main layout, navigation menu, and individual pages.
  3. Write Python scripts to process user navigation and render dynamic content.
  4. use JavaScript and JQuery to handle user interactions on the subnavigation menu.
  5. Combine all components to run a complete web application.

Project Setup

First, create a new directory for your project:

mkdir subnav_menu
cd subnav_menu

Next, install Flask, JQuery, and other required packages:

pip install flask jinja2 JQuery

Main Layout Template (templates/main.html)

Create a new folder called templates, and inside it, create a file named main.html. This will serve as the main layout for our web application:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}Subnavigation Menu{% endblock %}</title>
<!-- Add links to CSS and other resources -->
</head>
<body>
{% block content %}{% endblock %}

<!-- Include JQuery library -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<!-- Add custom JavaScript for the subnavigation menu -->
<script src="static/js/nav.js"></script>
</body>
</html>

Navigation Menu Template (templates/nav.html)

Create another file named nav.html inside the templates folder:

<nav id="subnav">
<ul>
{% for link in nav_links %}
<li><a href="{{ link.url }}" class="nav-link">{{ link.text }}</a></li>
{% endfor %}
</ul>
</nav>

Individual Page Templates (e.g., templates/home.html)

Create separate files for each individual page inside the templates folder, such as home.html, about.html, and so on:

{% extends 'main.html' %}

{% block title %}{{ page_title }}{% endblock %}

{% block content %}{{ page_content }}{% endblock %}

App Initialization (app.py)

In the project root, create a new file called app.py. This will contain our main application code:

from flask import Flask, render_template, url_for, request

app = Flask(__name__)
nav_links = [
{'text': 'Home', 'url': '/'},
{'text': 'About', 'url': '/about'},

Add more links as needed

]

@app.route('/')

def home():

return render_template('main.html', nav_links=nav_links, page_title='Home', page_content='Welcome to our website!')

@app.route('/about')

def about():

return render_template('main.html', nav_links=nav_links, page_title='About Us', page_content='Learn more about us here.')

... (add routes for other pages)

if __name__ == "__main__":

app.run(debug=True)


Now, run the application:

python app.py


Visit `http://127.0.0.1:5000/` in your web browser to see the subnavigation menu in action.

Worked Example

Let's extend our example by creating an additional page for displaying dynamic content based on user navigation and handle user interactions using JavaScript:

Add a New Page (templates/about.html)

Create a new file called about.html inside the templates folder:

{% extends 'main.html' %}

{% block title %}About Us{% endblock %}

{% block content %}
<h1>Welcome to Our Website</h1>
<p id="about-content">This is a brief description of our company and what we do.</p>

<!-- Add a button to show/hide more information -->
<button id="show-more">Show More</button>
{% endblock %}

Custom JavaScript for Subnavigation Menu (static/js/nav.js)

Create a new folder called static, and inside it, create a file named nav.js. This will contain our custom JavaScript code:

$(document).ready(function() {
// Handle navigation menu clicks
$('#subnav ul').on('click', 'li a', function(e) {
e.preventDefault();
var target = $(this).attr('href');
$('html, body').animate({ scrollTop: $(target).offset().top }, 500);
});

// Show/hide more information on the About page
$('#show-more').click(function() {
var aboutContent = $('#about-content');
if (aboutContent.hasClass('hidden')) {
aboutContent.removeClass('hidden');
$(this).text('Show Less');
} else {
aboutContent.addClass('hidden');
$(this).text('Show More');
}
});
});

Now, when you navigate to http://127.0.0.1:5000/about, you'll see the dynamic content for the About page, and clicking the "Show More" button will reveal or hide additional information.

Common Mistakes

1. Forgetting to render templates

Ensure that you call render_template in your route functions to display the HTML templates.

2. Incorrectly defining navigation links

Make sure that the URLs and text for each navigation link are correctly defined in the nav_links list.

3. Not properly handling user interactions with JavaScript

Ensure that you attach event listeners to the appropriate elements and handle user interactions accordingly using JavaScript or JQuery.

Practice Questions

  1. Add a new page called "Contact Us" with a form for users to send messages.
  2. Style the subnavigation menu using CSS to make it more visually appealing.
  3. Implement user authentication and restrict access to certain pages based on login status.
  4. Create a dropdown menu for the navigation bar when the screen size is small.
  5. Add animations or transitions to improve the user experience.

FAQ

Q: How can I customize the appearance of my subnavigation menu?

A: You can use CSS to style your navigation menu by targeting the `, , and elements, as well as their child elements like `. Additionally, consider using a pre-made design framework like Bootstrap for more advanced styling options.

Q: Can I use other web frameworks instead of Flask for creating a subnavigation menu in Python?

A: Yes, there are several other web frameworks available for Python, such as Django and Pyramid. However, Flask is a popular choice due to its simplicity and ease of use.

Q: How can I make my subnavigation menu responsive on different screen sizes?

A: You can use CSS media queries to adjust the layout of your navigation menu based on the screen size. For example, you might choose to hide some links or display them as a dropdown menu when the screen is too small.

Subnavigation Menu (Python Programming) | Python | XQA Learn