DEV Community

Mark Harless
Mark Harless

Posted on

Hello World with Flask

When I attended my coding boot camp late last year we learned Ruby and started with Sinatra before we moved onto Ruby on Rails. I remember not liking it too much but maybe that's because I was mentally exhausted from everything we were learning!

Now that I have graduated, I've been looking into Python a lot. I consider Python and Ruby to be very similar backend languages, however, Python is undoubtedly more popular. I remember searching for help for Sinatra but the blog posts that would show up in Google Search were from years ago! This is the main reason I switched from Ruby to Python: a stronger, more active community.

With that being said, Sinatra and Flask are two very similar web frameworks. They are the bare-bones counterparts to Ruby on Rails and Django — frameworks you've probably heard more about. While Flask can get you up and running for a small app, you have to do everything yourself. This may sound bad but it allows you, the developer, to have complete customization and control of your framework. I actually wrote Ninny Code! using Flask!

In this blog post, I'm going to show you how to create a very basic Flask app that returns "Hello World" when you visit it in your browser. Let's get started!

Python uses virtual environments to encapsulate your project. Instead of accessing anything and everything located in your computer, virtual environments localizes your production within the project. There are several ways to create a virtual environment but I found pipenv to be the easiest. Let's create an empty project directory and create our virtual environment:

mkdir helloworld
cd helloworld

pip install pipenv
pipenv shell
Enter fullscreen mode Exit fullscreen mode

The command pipenv shell creates our virtual environment. Again, this localizes our project into this directory. You'll notice it created a Pipfile for you. If you're new to pipenv, instead of running pip to install packages, you use pipenv instead. So let's install Flask then create our app.py:

pipenv install flask
Enter fullscreen mode Exit fullscreen mode
from flask import Flask
app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'
Enter fullscreen mode Exit fullscreen mode

And, honestly, that's it. This is the minimum amount of code you need to create a web app. In your terminal, enter flask run to start your server. You'll now see something like Running on http://127.0.0.1:5000 in your terminal. This is telling us that our site is now live and can be visited at that address in your browser (or localhost:5000)!

You should see the text "Hello World!". Congrats on making your first Flask application! If this tickles your fancy, I suggest you take a look at the (Flask docs) for a lot more info. I also think you should take a look at my public repo for Ninny Code! so you can get an idea of what a beefier Flask app looks like :D

Latest comments (1)

Collapse
 
steelwolf180 profile image
Max Ong Zong Bao

If your using Flask as a backend service you can just use FastAPI and use JavaScript frontend to make it work.