Flask Introduction | Make Websites Using Python | Introduction
Flask Introduction | Make Websites Using Python | Introduction
Flask is a micro web framework written in Python and based on the Werkzeug toolkit and Jinja2 template engine. It is BSD licensed. ... However, Flask supports extensions that can add application features as if they were implemented in Flask itself. - Wikipedia
Welcome to the new series of Flask. It is very important for Python programmers as it is the way to connect your Python code with front end, in easy words. This will be a series of tutorial covering Flask from basic.
What To Expect In This Series?
This series will help you learn Flask and will give you enough knowledge that you can build your own websites with Python. As said below you will need basic understanding of HTML, CSS and Python. I will try to explain each line of code in detail so beginners can also follow me in this series. Even if you don't understand those language you can still follow this series but I recommend having some knowledge of them.
Things You Will Need For This Series
- Basic Understanding Of Python
- Basic Understanding Of HTML & CSS
- Code Editor
- Python
Difference Between Django And Flask
Many people will argue between Django and Flask. Most of the time it depends on the type of you are gonna do. There are still some differences between Django and Flask that makes both good. If I tell in easy words, Flask allows us to do any task "our" way while Django restricts us to use "his" way to perform different tasks. Flask gives us for freedom compared to Django but many may argue that Django is still better, Why?. Most of the time Django way is better way but still I like to do things my way so Flask is for me.
Why Flask?
When you start coding in Python. you may start with so-called "Command Line Applications" which runs in command line interface. These commands or applications can be run through Shell or Terminal. If you want to build web applications or websites you may need to use frame works and Flask is one of them. Without frameworks like Python it is very hard to connect your Python code with front end but with frameworks it can become alot easier. One of the main advantages of using Flask is that you can still use those Python for loops, variables etc.
Features Of Flask
- Contains development server and debugger
- Integrated support for unit testing
- Restful request dispatching
- Uses Jinja2 templating
- Support for secure cookies (client side sessions)
- 100% WSGI 1.0 compliant
- Unicode-based
- Extensive documentation
- Google App Engine compatibility
- Extensions available to enhance features desired
Installing Flask
Installing Flask is very simple you can do it simply through terminal. Just type pip3 install flask --user
and Flask will be installed(It can also ask for password). Make sure you have a code editor installed because you will need it to continue in this series.
Starting Coding In Flask
After installing Flask, create a new file with any name you want and paste this code.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello, World!"
if __name__ == "__main__":
app.run()
Explanation
- First line simply just imports the Flask from Flask library, nothing super complicated about that.
- Second line assign variables "app" to "Flask" as a name to our application. Many people ask about this that why is "name" used, it is simply to help Flask to know that is is gonna be name of our application.
- Third line is about routing which is one of the most important things in Flask. I will not go in detail in this post but to give you basic idea what that piece of code does, It just simply tells the browser that this path exist which is in this case "/" also known as homepage.
- Fourth code creates a function to tell what should browser do when user goes above path.
- Fifth piece of code just simple tells the browser to return a string "Hello, World!". So whenever user goes to "/" path the browser will return a string "Hello, World!".
- After that is the code that will run the server. "if name == "main":" is just to make sure that name is name of the main app that just adds a layer of protection and then runs it with code "app.run()".
- No problem if you don't understand some parts, I will explain them as we go so you will have better understanding. You can start the code and go to http://127.0.0.1:5000/ to see the website. It's now on local server that's why you will need that IP Address to access it.
That was just a simple code in upcoming tutorials you will get more into Flask so stay tuned for that!
Posted on Utopian.io - Rewarding Open Source Contributors
Your contribution cannot be approved because it does not follow the Utopian Rules.
According to the blog posts rules:
Hi. Your post does not fit the blog posts category, and it's more in line with the tutorials category. Consider to write next post of your series in the Tutorials category.
Also, You must include the source of the used images, and some parts of your post, like the Explanation section, could look better.
You can contact us on Discord.
[utopian-moderator]
No Problem, I can understand that. Thanks for your time for reviewing this post.
Thanks. I'm getting started with Python and Flask. I appreciate your explanation of the code example you included.