Web developement with python #1 : Flask initialization and Routing system

in #utopian-io5 years ago

Repository

https://github.com/python

What Will I Learn?

  • Initialize web development with Flask
  • Routing system
  • Parameters in routing

Requirements

  • Basic Python
  • Install Python 3
  • Install Flask

Resources

Difficulty

Basic

Tutorial Content

This tutorial will discuss something different from my previous tutorial, in this tutorial, we will explore one of the most popular programming languages, Python. If the previous tutorial was focused on web development using JavaScript, in this tutorial I will share how web development uses the python based on what I have learned so far. I will not discuss python from the basics. I assume you understand the basics of python because the python language is not much different from other languages. In this tutorial, I will use tools from FLASK. What is a flask? and how to start web development with python ?, okay we just start this tutorial.

What is Flask?

I will explain the flask briefly because that is not the essence of this tutorial. Flask is a Python Micro framework. Why Flask is called a micro framework ?. because the flask doesn't have clear rules or conditions when configuring it. so developers are free to make settings or configurations and coding methods that are in accordance with the wishes of each and you can also add additional extensions. Flask is widely used as a tool to help us make web development easier. Flask has been widely used so you don't have to worry about stability issues.

  • Install Flask

Of course, we have to install the flask so we can use it. Make sure you have installed Python first. to install python you can visit the official website at https://www.python.org/. After you have installed python, We have to install Flask in the following way:

Install Flask

pip install flask

pip is like a package manager for python, If you are from PHP you know composer and if you are from javascript you are definitely familiar with NPM.

Noted: You must add env. variable into your computer path. So that your computer can recognize commands from the flask.

First web initialization

We have installed the flask, now we will initialize our first web app. We can make simple routing to test whether our Flask has been successfully installed properly. The following is an example:

// import flask
from flask import Flask

//Use flask
app = Flask(__name__)

//Create routing in flask
@app.route('/welcome')
def myFunction():
        return 'Hallo utopian'

The above code is a normal structure that will be an example. The first thing we do is import the flask from flask import Flask and then we can defined flask app = Flask(__name__) and use the routing with @app.route('/nameOfRoute') and we can set the functions that we will use in this tutorial I use the function myFunction() in routing /welcome, the function just return 'Hello utopian'

  • Run on the Server

Because we will create a web application, of course, we need a server to run the application, we can run the server in the flask in the following way:

> set FLASK_APP=app.py

> flask run

OR

> export FLASK_APP=app.py

> flask run

The above code is used to set env. from FLASK_APP. If we have set env. FLASK_APP we can use the flask run to run it on the server. We can see the example in the picture below:

Screenshot_12.png

as we see in the picture above the server runs on the port http://127.0.0.1:5000 and routing we are at /welcome

ezgif.com-video-to-gif (5).gif

Restart the server automatically

When we create a web app, of course, we often want to see changes to our application. but in the flask, we cannot see the changes automatically. but we change it to automatic by activating the debug mode. for more details, we can see in the picture below:

Screenshot_1.png

ezgif.com-video-to-gif.gif

If we run our server and we will see that we don't need to restart the server to see changes to our code.

Dynamic routing

At the top we have made static routing /welcome and in this section, we will make dynamic routing, as we often use in PHP we certainly often use dynamic routing depending on the parameters we passed. We can use it like the code below:

@app.route('/welcome/<user>')
def paramFunc(user):
        return 'Welcome %s' %user

We can create dynamic routing using <> and then we can pass in function paramFunc(user), to display it we can determine the data type, which is a string %s and to print %user.

ezgif.com-video-to-gif (1).gif

  • Integer parameters

Besides strings %s we can also pass parameters with other data types such as interger. We can set parameters in routing in the type of interger like this:

@app.route('/profile/<int:user_id>')
def profileFunc(user_id):
        return 'Your id is %d' %user_id

for integer data types we can do it like this <int:paramName> and because the data type is a integer, we can print out like this %d'. If we run it on our application then when the parameter we have passed is not an integer then routing will NOT FOUND.

ezgif.com-video-to-gif (2).gif

We can see in the picture above we have learned flask and routing, we are still learning the basics about flask and python for that I will continue on in the next tutorial series, in the next tutorial, I will introduce a templating system which will certainly be very useful for building web development. I hope this tutorial can give you an idea of how to create web applications in python language. That is all from me. thank you

Proof of work done

https://github.com/milleaduski/python-web-app

Sort:  

Thank you for your contribution.

  • I haven't heard of Flask before, it's interesting to see such frameworks.
  • I believe the tutorial is quite basic. Flask is well documented and has tons of online tutorials that are available covering your and more thorough work. I would advise you provide something more unique to the table.
  • I like the fact that you document and show screenshots of the different steps, useful to your reader.
  • I suggest you try to improve the overall language and punctuation of the post.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you for your review, @mcfarhat! Keep up the good work!

Hi @duski.harahap!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Congratulations @duski.harahap! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You received more than 3000 upvotes. Your next target is to reach 4000 upvotes.

Click here to view your Board of Honor
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:

Meet the Steemians Contest - The results, the winners and the prizes
Meet the Steemians Contest - Special attendees revealed
Meet the Steemians Contest - Intermediate results

Support SteemitBoard's project! Vote for its witness and get one more award!

Hey, @duski.harahap!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.033
BTC 64386.10
ETH 3142.17
USDT 1.00
SBD 3.98