Everything you need as a full stack developer

Flask Hello World with basic app.route demonstration

- Posted in Flask by

**TL;DR To create a basic "Hello World" application using Flask, install Flask via pip and then create a new file named app.py with the following code:

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
    return 'Hello, World!'
if __name__ == '__main__':
    app.run()

Run the application by executing python app.py, then open a web browser and navigate to http://localhost:5000/ to see "Hello, World!" displayed on the page.**

Creating Your First Flask Application: A Step-by-Step Guide

As we venture into the world of web development, it's essential to understand that building a robust application from scratch can be an overwhelming task for beginners. That's where frameworks like Flask come in – they provide a foundation upon which you can build your web applications with ease.

In this article, we'll explore how to create a simple "Hello World" application using Flask, and take it a step further by demonstrating basic routing concepts.

Installing Flask

Before diving into the code, let's ensure that we have Flask installed in our Python environment. You can install it via pip:

pip install flask

With Flask up and running, we're ready to build our first application!

Creating Your First Application

Create a new file named app.py and add the following code:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == '__main__':
    app.run()

Let's break down what we've just created:

  • We imported Flask from its package.
  • An instance of the Flask application was created and stored in app.
  • The @app.route('/') decorator is used to associate a function with a URL path. In this case, it maps / (the root URL) to the hello_world() function.
  • The if __name__ == '__main__': block ensures that our application only runs when the script is executed directly (not if it's imported as a module in another script).

Running Your Application

Save the file and run your Flask application by executing:

python app.py

Open up a web browser and navigate to http://localhost:5000/. You should see "Hello, World!" displayed on the page.

Basic Routing Demonstration

Now that we have our first "Hello World" application up and running, let's explore some basic routing concepts. Update your app.py file with the following code:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

@app.route('/about')
def about_us():
    return 'This is a short description of our company.'

if __name__ == '__main__':
    app.run()

Here's what changed:

  • We've added another route /about that maps to the about_us() function.
  • This new function returns a string describing your company.

Run the application again by executing python app.py. Now, when you navigate to http://localhost:5000/, you should see "Hello, World!". Visit http://localhost:5000/about to view the about page description.

Conclusion

Congratulations! You've just taken your first steps in building a Flask application. We started by installing Flask and creating our first "Hello World" application using basic routing concepts. Remember that this is just the beginning of your journey as a web developer, and there's much more to explore with Flask!

In the next article, we'll dive deeper into advanced routing features, templates, and database integration – but for now, enjoy building simple applications with Flask!

Fullstackist aims to provide immersive and explanatory content for full stack developers Fullstackist aims to provide immersive and explanatory content for full stack developers
Backend Developer 103 Being a Fullstack Developer 107 CSS 109 Devops and Cloud 70 Flask 108 Frontend Developer 357 Fullstack Testing 99 HTML 171 Intermediate Developer 105 JavaScript 206 Junior Developer 124 Laravel 221 React 110 Senior Lead Developer 124 VCS Version Control Systems 99 Vue.js 108

Recent Posts

Web development learning resources and communities for beginners...

TL;DR As a beginner in web development, navigating the vast expanse of online resources can be daunting but with the right resources and communities by your side, you'll be well-equipped to tackle any challenge that comes your way. Unlocking the World of Web Development: Essential Learning Resources and Communities for Beginners As a beginner in web development, navigating the vast expanse of online resources can be daunting. With so many tutorials, courses, and communities vying for attention, it's easy to get lost in the sea of information. But fear not! In this article, we'll guide you through the most valuable learning resources and communities that will help you kickstart your web development journey.

Read more

Understanding component-based architecture for UI development...

Component-based architecture breaks down complex user interfaces into smaller, reusable components, improving modularity, reusability, maintenance, and collaboration in UI development. It allows developers to build, maintain, and update large-scale applications more efficiently by creating independent units that can be used across multiple pages or even applications.

Read more

What is a Single Page Application (SPA) vs a multi-page site?...

Single Page Applications (SPAs) load a single HTML file initially, handling navigation and interactions dynamically with JavaScript, while Multi-Page Sites (MPS) load multiple pages in sequence from the server. SPAs are often preferred for complex applications requiring dynamic updates and real-time data exchange, but MPS may be suitable for simple websites with minimal user interactions.

Read more