create account

Web developement with python #1 : Flask initialization and Routing system by duski.harahap

View this thread on: hive.blogpeakd.comecency.com
· @duski.harahap ·
$14.31
Web developement with python #1 : Flask initialization and Routing system
#### 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
- Python - https://www.python.org/
- Flask - http://flask.pocoo.org/

#### 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'```
<br>
- **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](https://ipfs.busy.org/ipfs/QmXsxmrVnWEejr1PpXG4y1wJHc5AC51mZsZ9D1LHKcq96T)

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](https://ipfs.busy.org/ipfs/QmfHTxnxAW1ihm1xNkLppMPhW1JoLsJKm82wi76ocr2YgD)

### 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](https://ipfs.busy.org/ipfs/QmZyzg8jykDtrRGqW8ZZQ3DTP6GryPHerQrdKjZVsPtthz)

![ezgif.com-video-to-gif.gif](https://ipfs.busy.org/ipfs/QmNdzUDJ3ybvBMHKKxwRgxMTGttnNHRvq4Ln7rdx6eeaSd)

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](https://ipfs.busy.org/ipfs/QmewsZrVXDEToYrER9XGnskeUaqe94Js1KV9HB2Rbo6otC)
<br>
- **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](https://ipfs.busy.org/ipfs/QmZtH5pBn4F5737m8aTseptQ6F1Z1U7cj668tmzkMwEqGa)



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
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 5 others
properties (23)
authorduski.harahap
permlinkweb-developement-with-python-1-flask-initialization-and-routing-system-1542726589553
categoryutopian-io
json_metadata{"app":"steeditor/0.1.2","format":"markdown","image":["https://ipfs.busy.org/ipfs/QmXsxmrVnWEejr1PpXG4y1wJHc5AC51mZsZ9D1LHKcq96T","https://ipfs.busy.org/ipfs/QmXsxmrVnWEejr1PpXG4y1wJHc5AC51mZsZ9D1LHKcq96T","https://ipfs.busy.org/ipfs/QmfHTxnxAW1ihm1xNkLppMPhW1JoLsJKm82wi76ocr2YgD","https://ipfs.busy.org/ipfs/QmZyzg8jykDtrRGqW8ZZQ3DTP6GryPHerQrdKjZVsPtthz","https://ipfs.busy.org/ipfs/QmNdzUDJ3ybvBMHKKxwRgxMTGttnNHRvq4Ln7rdx6eeaSd","https://ipfs.busy.org/ipfs/QmewsZrVXDEToYrER9XGnskeUaqe94Js1KV9HB2Rbo6otC","https://ipfs.busy.org/ipfs/QmZtH5pBn4F5737m8aTseptQ6F1Z1U7cj668tmzkMwEqGa"],"tags":["utopian-io","tutorials","python","flask","web"],"users":["app"],"links":["https://github.com/python","https://www.python.org/","http://flask.pocoo.org/","https://www.python.org/.","https://github.com/milleaduski/python-web-app"]}
created2018-11-20 15:09:54
last_update2018-11-20 15:09:54
depth0
children5
last_payout2018-11-27 15:09:54
cashout_time1969-12-31 23:59:59
total_payout_value10.804 HBD
curator_payout_value3.501 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6,346
author_reputation60,094,717,098,672
root_title"Web developement with python #1 : Flask initialization and Routing system "
beneficiaries
0.
accountutopian.pay
weight500
max_accepted_payout100,000.000 HBD
percent_hbd10,000
post_id75,621,241
net_rshares24,092,479,129,785
author_curate_reward""
vote details (69)
@mcfarhat ·
$5.73
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](https://join.utopian.io/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](https://review.utopian.io/result/8/31121433).

---- 
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
πŸ‘  , , , , , , , , , ,
properties (23)
authormcfarhat
permlinkre-duskiharahap-web-developement-with-python-1-flask-initialization-and-routing-system-1542726589553-20181120t163436884z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/8/31121433","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-11-20 16:35:06
last_update2018-11-20 16:35:06
depth1
children1
last_payout2018-11-27 16:35:06
cashout_time1969-12-31 23:59:59
total_payout_value4.338 HBD
curator_payout_value1.389 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,004
author_reputation150,651,671,367,256
root_title"Web developement with python #1 : Flask initialization and Routing system "
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,625,795
net_rshares9,197,726,136,029
author_curate_reward""
vote details (11)
@utopian-io ·
Thank you for your review, @mcfarhat! Keep up the good work!
properties (22)
authorutopian-io
permlinkre-re-duskiharahap-web-developement-with-python-1-flask-initialization-and-routing-system-1542726589553-20181120t163436884z-20181123t062203z
categoryutopian-io
json_metadata"{"app": "beem/0.20.9"}"
created2018-11-23 06:22:03
last_update2018-11-23 06:22:03
depth2
children0
last_payout2018-11-30 06:22:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length60
author_reputation152,955,367,999,756
root_title"Web developement with python #1 : Flask initialization and Routing system "
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,770,609
net_rshares0
@steem-ua ·
#### 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](https://discord.gg/KpBNYGz)**
properties (22)
authorsteem-ua
permlinkre-web-developement-with-python-1-flask-initialization-and-routing-system-1542726589553-20181120t165938z
categoryutopian-io
json_metadata"{"app": "beem/0.20.9"}"
created2018-11-20 16:59:39
last_update2018-11-20 16:59:39
depth1
children0
last_payout2018-11-27 16:59:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length292
author_reputation23,214,230,978,060
root_title"Web developement with python #1 : Flask initialization and Routing system "
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,627,117
net_rshares0
@steemitboard ·
Congratulations @duski.harahap! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

<table><tr><td>https://steemitimages.com/60x70/http://steemitboard.com/@duski.harahap/voted.png?201811201534</td><td>You received more than 3000 upvotes. Your next target is to reach 4000 upvotes.</td></tr>
</table>

<sub>_[Click here to view your Board of Honor](https://steemitboard.com/@duski.harahap)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>



**Do not miss the last post from @steemitboard:**
<table><tr><td><a href="https://steemit.com/steemfest/@steemitboard/meet-the-steemians-contest-the-results-the-winners-and-the-prizes"><img src="https://steemitimages.com/64x128/https://cdn.steemitimages.com/DQmeLukvNFRsa7RURqsFpiLGEZZD49MiU52JtWmjS5S2wtW/image.png"></a></td><td><a href="https://steemit.com/steemfest/@steemitboard/meet-the-steemians-contest-the-results-the-winners-and-the-prizes">Meet the Steemians Contest - The results, the winners and the prizes</a></td></tr><tr><td><a href="https://steemit.com/steemfest/@steemitboard/meet-the-steemians-contest-special-attendees-revealed"><img src="https://steemitimages.com/64x128/https://cdn.steemitimages.com/DQmeLukvNFRsa7RURqsFpiLGEZZD49MiU52JtWmjS5S2wtW/image.png"></a></td><td><a href="https://steemit.com/steemfest/@steemitboard/meet-the-steemians-contest-special-attendees-revealed">Meet the Steemians Contest - Special attendees revealed</a></td></tr><tr><td><a href="https://steemit.com/steemfest/@steemitboard/meet-the-steemians-contest-intermediate-results"><img src="https://steemitimages.com/64x128/https://cdn.steemitimages.com/DQmeLukvNFRsa7RURqsFpiLGEZZD49MiU52JtWmjS5S2wtW/image.png"></a></td><td><a href="https://steemit.com/steemfest/@steemitboard/meet-the-steemians-contest-intermediate-results">Meet the Steemians Contest - Intermediate results</a></td></tr></table>

> Support [SteemitBoard's project](https://steemit.com/@steemitboard)! **[Vote for its witness](https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1)** and **get one more award**!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-duskiharahap-20181120t173438000z
categoryutopian-io
json_metadata{"image":["https://steemitboard.com/img/notify.png"]}
created2018-11-20 17:34:36
last_update2018-11-20 17:34:36
depth1
children0
last_payout2018-11-27 17:34:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,175
author_reputation38,975,615,169,260
root_title"Web developement with python #1 : Flask initialization and Routing system "
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,628,800
net_rshares0
@utopian-io ·
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](https://chrome.google.com/webstore/detail/steemplus/mjbkjgcplmaneajhcbegoffkedeankaj?hl=en) or [Steeditor](https://steeditor.app)).

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

<a href='https://steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
properties (22)
authorutopian-io
permlinkre-web-developement-with-python-1-flask-initialization-and-routing-system-1542726589553-20181121t022213z
categoryutopian-io
json_metadata"{"app": "beem/0.20.9"}"
created2018-11-21 02:22:15
last_update2018-11-21 02:22:15
depth1
children0
last_payout2018-11-28 02:22:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length595
author_reputation152,955,367,999,756
root_title"Web developement with python #1 : Flask initialization and Routing system "
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id75,650,289
net_rshares0