Saturday, June 11, 2016

Python Flask Tutotials For Beginners Lesson 2 - Flask Hello World and Route Debug Change Port

Hello My dear friends in previous tutorial we have discuss how to setup all the requirements that we need to create video upload web api.so in this tutorial we are going to learn few things.

if images are not clear please click on image to zoom

  • Run Flask Hello world application
  •  
  • Add Debug mod to Flask application
  •  
  • Change Flask Route
  •  
  • Change Host and Port

 Run Flask Hello world application


first open pycharm and goto File and new project then select flask from the menu and give a name and folder to save file.
 

then it will automatically created the hello world program for you


then right click and select run.or you can use alt+shift+f10

then it will give massage it will run on 127.0.0.1:5000

this is run on local host port 5000


so lets type 127.0.0.1:5000 in web browser.

it give a result like this


so you get hello world in Flask.

ok now take a look at the code

from flask import Flask

app = Flask(__name__)



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


if __name__ == '__main__':
    app.run()
  

from flask import Flask

this means we get only Flask module from flask package.
otherwise we can import Flask it will import the all the modules inside the flask package.

app = Flask(__name__)
this means this python program run as flask app named app you can use app or application or whatever name you like

@app.route('/')
This is route this point the function to return value .this is root value that means

if you type 127.0.0.1:5000 this will return Hello World as return

def hello_world():
    return 'Hello World!' 
this is python function

you have to careful to maintain intendant otherwise it will not execute.maintain the suitable spaces.

if __name__ == '__main__':
    app.run()

this means run the main function

Add Debug mod to Flask

when you add debug mod you can run application continuously when the code is changed. 

app.debug=True



Change Host and Port in Python Flask

you can change change host and port in a Flask app using 
host='your IP or host name',port=port number
if __name__ == '__main__':
    app.run(host='127.10.20.30',port=8080) 


Change Route in Flask


route is the pointing point to the python flask function.when you route it will point the relevant function and it will return the output of the function.

@app.route('/')

now we create new function called sayhi().to print hello sameera 

@app.route('/sayhi')
def say_hi():
    return 'welcome sameera ! :) '


 so we are going through the 127.10.20.30:5000/sayhi

this will give the output welcome sameera ! :) in web browser



Hope you enjoy tutorial.see you in next lesson

if you like this tutorial please share in facebook,google+ twitter .If you have any question please leave a comment




Python Flask Tutotials For Beginners Lesson 1 - install and set up Python Flask

Lesson 1 - install and set up Python Flask

Howdy folks after long time .i decided to start new tutorial series about, python Flask framework.in this tutorial series I'm going to teach you ,how to create video hosting flask web server with mango db ,Dockers ,apache reverse proxy and nginx reverse proxy.i hope you will learn a lot.OK lets start to work

things we needed
  • ubuntu
  • pycharm as development IDE
  • python 2.7 (im using python 2.7 version)Ubuntu have built-in python
  • Dockers
  • Apache2 or nginx im going to teach you both
  • Mongo DB
  • Open SSL for convert http to https
im going though all the lesson from very first beginning.

ok lets setup pycharm as our development IDE


Install pycharm

 go to jetbrains pycharm website https://www.jetbrains.com/pycharm/download/#section=linux and download professional or community version.

then extract it to anywhere you like then you can run PyCharm using bash pycharm.sh command to to do this you need to open extracted pycharm folder using terminal.you can use cd command to change the directory and go to pycharm folder then it will start the pycharm.


How to add Pycharm to Ubuntu Laucher


when you open pycharm. Icon is appear on ubuntu Launcher then right click on that icon and click lock to launcher.
and also goto Tools -->Create Desktop Entity 


How to install PIP in ubuntu


sudo apt-get update

sudo apt-get -y install python-pip

this will install pip for you.pip help you to install python modules easy.


How to install Docker in Ubuntu


sudo apt-get update

sudo apt-get install docker-engine

then start the docker daemon using

sudo service docker start

How to install Apache2 on Ubuntu

 Apache2 is the webserver and also apache2 can work as reverse proxy.i will discuss this later.

sudo apt-get update 

sudo apt-get install apache2 apache2-doc apache2-utils


How to install Nginx on Ubuntu

Nginx also web server like apache2.but nginx is light weight rather than apache2 .Nginx require low server requirement.and also memory consumption also lower than apache2.and also nginx can serve as webserver.reverse proxy and load balancer. 

sudo apt-get update

sudo apt-get install nginx


How to install Flask on ubuntu

now you have installed PIP in your ubuntu machine.so we need to install Python Flask on your Desktop so we use pip to install Flask for you.

open terminal and type

 

How To install Python Flask on ubuntu


pip install flask

flask is the micro framework.you can do anything using flask.it is lightweight.it include the core of the framework.

 

How to install Mongodb on Ubuntu

 

sudo apt-get update

sudo apt-get install -y mongodb-org



How to install Pymongo on ubuntu


now we need pymongo driver.to communicate flask app with mongo db

python -m pip install pymongo

ok now we are setup and install althe things we needed .so lets gets started.
see you in next tutorial

How to run your first flask application.

If you like this tutorial Please Share and leave a comment below