2. Introduction
This presentation covers the essentials of building back-end
applications using Flask. We'll dive deep into routing, templates, and
building a simple multi-page app, enabling a dynamic user experience.
4. Routing:
• App Routing means mapping the URLs to a specific
function that will handle the logic for that URL.
• Example: In our application, the URL (“/”) is associated
with the root URL.
5. from flask import Flask
app = Flask(__name__)
# Pass the required route
@app.route("/hello")
def hello():
return "Hello, Welcome to GeeksForGeeks"
@app.route("/")
def index():
return "Homepage of GeeksForGeeks"
if __name__ == "__main__":
app.run(debug=True)
6. Templates:
• A flask template is a template of a webpage that can be used by web developers to reduce the
hectic work of writing the code for each webpage of a website.
• For example, web developers can create a template that has headers like a navbar and footer
which is the same for all web pages. so instead of writing code for the header and footer of
every webpage, we can use that template.
render_template() method :
• The render_template() method, allows us to fetch the HTML files, and, pass them to the browser.
• Hence, instead of rendering, raw string or inline HTML code, from the backend code to the browser, we can have
a separate HTML file, passed to the client. The steps followed, to use the same, are as follows.
9. Static files:
• Flask allows you to serve static files like CSS, JavaScript, and images through a
dedicated folder, typically named 'static’.
• These files are essential for enhancing the visual aspects of the web application
and improving user experience.
• In your Flask application, you can refer to these static files in your templates
using the url_for() function, making it easy to manage paths efficiently and ensure
that your assets load correctly across different environments.
11. Creating a multi-
page Flask app
(e.g., Home,
About, Contact)
To create a multi-page application in Flask, you define several routes
that correspond to different views in your app, such as Home, About,
and Contact. Each route is linked to a dedicated function that renders a
specific template. By organizing your app this way, you create a
cohesive user experience, allowing for seamless navigation between
different sections of the application, enhancing usability and
engagement.
12. Passing data from the back-end to the front-
end
Flask enables seamless data transfer from the back-end to the front-end using
template rendering.
Variables defined in your Flask route functions can be passed to your templates
when rendering them, allowing you to display dynamic content to users.
This interaction can include user input, database query results, or any other data
necessary to enhance the application's functionality and user interface.
13. Building a small Flask app with multiple pages
and dynamic content
Building a small Flask application involves integrating all the concepts learned, such as routing, templates,
and serving static files. Start by setting up your route handlers for each page, ensuring they render the
correct templates while passing necessary data. Incorporate dynamic elements that adjust based on user
interactions or other inputs, allowing for a richer user experience. Through practice, students can solidify
their understanding of Flask and create functional web applications that reflect their skills.
14. Conclusions
The presentation highlighted key aspects of building back-end
applications using Flask, including essential routing, templates,
and static file management for enhanced web functionality.
Students are encouraged to apply this knowledge to develop
multi-page applications with dynamic content, improving their
programming skills and understanding of the Flask framework.
15. CREDITS: This presentation template was created by Slidesgo, and
includes icons by Flaticon, and infographics & images by Freepik
Thank you!
Do you have any questions?