DEV Community

Cover image for Flask - A Quick Guide
gupta-niharika
gupta-niharika

Posted on • Updated on

Flask - A Quick Guide

Hey there developers! How's your day going? :)

This post is for those who want to create a basic Flask app pronto !

What is Flask?

Flask is a micro web framework written in Python meant for easy and quick development of web apps.

Quick - Launch

The official documentation is always necessary, however it may not be handy when you have the deadline ticking over your head. You could be having cool ideas yet be unsure of how to go about them, and knowledge of other frameworks would only add to the confusion. This could get frustrating.

So, how could you quickly skip the basic setup and jump into the main section to start modeling your ideas?

Starting your Flask App

  • Make sure you have python and a good code editor installed on your laptop which would be the base of our construct.
  • Now, navigate to the project directory, either from your files or from Command Prompt.
  • Create a virtual environment where you can work on this project independently, without worrying about the quirks of other projects with:
py -m venv env 
Enter fullscreen mode Exit fullscreen mode

where env is the name of the environment. You could change its name if you
want.

  • Activate the environment with:
env\Scripts\activate
Enter fullscreen mode Exit fullscreen mode
  • Install flask in this environment with:
pip install flask
Enter fullscreen mode Exit fullscreen mode
  • To install a specific version of flask, simply mention it :
pip install flask==0.6.1
Enter fullscreen mode Exit fullscreen mode
  • In case you get a pip warning, you might have to upgrade pip :
python -m pip install --upgrade pip
Enter fullscreen mode Exit fullscreen mode

I always upgrade pip when I install flask for my projects.

  • In your text editor, simply copy-paste the following code and save the file as app.py
from flask import Flask
app = Flask(__name__)

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

if __name__ == '__main__':
    app.run()
Enter fullscreen mode Exit fullscreen mode
  • Now in the terminal, set the environment variable for your app and you are good to go !
set FLASK_APP = app.py
flask run
Enter fullscreen mode Exit fullscreen mode

Alternatively, you could run it directly by:

python app.py
Enter fullscreen mode Exit fullscreen mode

With that we have our localhost running, with the following message expected to show up on the terminal

* Debugger is active!
* Debugger PIN: XXX-XXX-XXX
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Enter fullscreen mode Exit fullscreen mode

Voila ! And there you go, with your basic flask app ready to be exploited for your game.

Top comments (0)