Navigation Bars in Flask
Navigation across HTML pages is quite a common way to work across pages. The client-side navigation approach is commonly used in HTML. However, having custom server-end navigations can provide greater flexibilities as far as customizations are concerned. This article walks you through a way to integrate Server end navigation using Navbars in Flask.
This module is used to build navigation bars in the flask application.
- Allows server end navigation from one page to another.
- Helps to define HTML classes and Navigation items from Python.
To install this module type the below command in the terminal.
pip install Flask-Navigation
nav.Bar(name, items) : Initializing name of navigation class with items definition.
nav.Item( label, url, args) : Assigns items or lists for each bar. Label name, its linked page URL and extra args like parameter and value of dictionary.
After installation, the next step is to initialize the app context with Navigation(), and define the basic HTML to navigate. Navigation bars also need to be defined in the code using Bar(), with takes position and list of items are parameters of Navigation Bars. Each Item is input with its label, URL, and parameters passed in the URL.
Step 1: Importing Libraries, adding app context and Initializing Navigation Class Object.
Step 2: Adding Navigation Definition
Step 3: Adding flask Routes and run application.
In this, for loop is used to iterate to render all the items defined under the top bar, with their url and labels.
While opening navpage.html, on click of each item, the page is navigated to the required URL as configured for navigation.
In navpage.html, each item of navbar is iterated and rendered. Onclick, class is activated and anchor tag takes to the defined path, the linked pages.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course