Friday, September 22, 2017

How to setup Python Flask on Lubuntu


Flask is a micro framework for python programming language, if you already know basic of python, the next step is to learn the framework. In this article i will show you how to setup a new project with python flask framework on lubuntu operating system.

Since python compiler already comes with the installation of lubuntu/ubuntu, there is no need to install python. But you are going to need python dependency package manager which called pip. Using pip you can install library for python including flask framework.

How to install pip on lubuntu
  • open command line on lubuntu (press CTRL + ALT + T)
  • run this command to install pip
  • sudo apt-get install python-pip


How to install flask and create new project
  • install Flask using pip
  • sudo pip install Flask
  • create new file called app.py
  • copy paste this code to app.py
  • from flask import Flask
    app = Flask(__name__)
    
    @app.route("/")
    def hello():
        return "Hello World!"
    
    if __name__ == '__main__':
     app.run()
  • run the program using this command
  • python app.py
  • open localhost port 5000 on your browser and see what happen http://127.0.0.1:5000/.

Alright you have just created a hello world program using python flask, this is a good start, you can add more url by adding @app.route, like this:
@app.route("/home")
def home():
     return "This is home page"

Go ahead try http://127.0.0.1:5000/home, note that you need to restart the server every time you change the code. You can put the application into debugging mode, so you don't need to restart the server every time.

To enable debug mode on flask, simply add parameter debug=True on the app.run(), like this:
if __name__ == '__main__':
 app.run(debug=True)

What you have learn so far is the basic of python flask framework, flask is a micro framework good for creating APIs, but also can be use to create websites. For more info read flask documentation and try some projects available on the internet.

No comments:

Post a Comment