404 Error Handling in Flask:

Whenever a page is not found a 404 error is shown. This occurs because the owner might have changed the URL or the page has been deleted. All sites should have Custom error pages in Flask Python for Data Science in order to avoid the showcase of the default error page which is not at all aesthetic.

Customized error page of PST Analytics

error page in python for data science

This page is not like the default page. The customized page provides the option of going back to the home page and also searching for something else.

Flask helps a way of handling the error and returns the customized error page.

In order to use the Custom error page, we need to download and import flask. Flask can be imported using the following commands in CMD.

pip install flask

apppy

Flask module helps us to make a Python file to define all routes and functions. In the example below, app py is the Python file, and in it, we have defined the main page (‘/’) and the error handler function which is flask function and is passed 404 error as a parameter.

python data science

Whenever the user opens a broken link, a 404 html file will be returned.

404 html:

The code given below will export header and navbar from header html.

Redirecting to home page after 5 seconds:

This code will remain same as app py code. And the code will show the custom page which we have created, and after 5 seconds it redirects the user to the home page.

Sample header html:

The sample header html includes navbar. It is made up of bootstrap. We can make one of our own also. Need to refer to the below code for bootstrap documentation.

We will see a custom error page when we type the codes given.

If you want to learn more about flask python in data science then you can check this and this as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.