DEV Community

Cover image for Basics of flask for creating RESTful web API's
Jayesh Kale
Jayesh Kale

Posted on • Edited on

Basics of flask for creating RESTful web API's

Did you know what flask is?, It's possible that you heard this word at least once if you have something to do with python. If you don't know then you're at right place. This will be three part series in 1st article we will discuss what flask is? and create hellow world app,
in next article we will create sample app called photo bucket explaining creation of project step by step, whereas in futher post we will structure our project. Don't worry if you didn't know any of this term mentioned; i will explain them briefly where ever possible. so lets get started.

Flask

Introduction

what is flask?

Flask is a micro-web framework developed by pallets projects for python to get started with creating api's quickly and easily.

lets break down above to understand more efficiently:

  1. micro meaning line of code for flask framework is less than what others offer. In practical flask doesn't enforce you too use only what flask provide or what should be directory structure. You can even make flask application in single python file or split it into multiple modules, however making application in single python file is not recommended as it will get utterly complex to manage as application grows. Main point to notice here is that flask doesn't come battery included so whats that actually mean? This means that like other framework it doesn't enforce you to only use framework specific ORM/ODM and drivers instead it lets you choose. Okay so lets you choose? suppose you want to include text-search support in your application for that you need search engine such as whoosh, elastic search other than that some databases also provide you with text search like mongodb now the question arises what will work with flask -> all of them. and that's the beauty of this framework.
  2. framework are the piece of code which consist regularly used functionality so you don't have to be code everything from scratch. for example, you use print() function in any language that print function is consist the logic detecting the string and variable as argument and then showing it on screen so you don't have to write everything and that will save your time and boost productivity. So framework are like predefined stuffs for you with some advanced features.

I think you have the idea now what flask is. so you can go to next topic. before that you will have to install flask do it with this command pip install flask

note: flask work on extension to provide extra functionality there are plenty of extension on python package index you can search extension using flask-packagename. to learn more about extension hit this link.

Hello World

app.py

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return "Hello World!"
if __name__ == "__main__":
    app.run()
Enter fullscreen mode Exit fullscreen mode
  • importing Flask class from flask module.
  • We are registering variable app as instance of flask
  • @app.route is decorator function used to provide path '/' or you can write '/helloworld' after the localhost address.
  • return statement to output hello world! as string on browser.
  • app.run() to run application

lets run it.
terminal
browser output

whoillaa pat on your back you just created api in flask.

Download Flask cheatsheet here

Top comments (0)