Apply for Zend Framework Certification Training

Python




< Create an border of registration form in python using tkinter Last >



Installation
Step -1  Install Flask using pip

pip install Flask
Step -2 Create an simple flask server
from flask import Flask 
app = Flask(__name__)
@app.route('/',methods=['GET','POST'])
def home():
    return "Server has started at Home ....." 

@app.route('/aboutus',methods=['GET','POST'])
def abouttus():
    return "About us path executed ....." 

if __name__ == '__main__':
    app.run(host='0.0.0.0',port=1208)


Save this file as app.py (or any other filename you want) and go to the terminal and type python app.py (i.e. python .py )

You should see something like this:

* Running on http://0.0.0.0:1028/ (Press CTRL+C to quit)
Launch any web browser and go to http://localhost:105/hello/ to see the app in action.

Now, let’s understand the working of the code line-by-line:

from flask import Flask → Import the Flask class

app = Flask(__name__) → Create an instance of the class

@app.route('/', methods=['GET', 'POST']) → We use the route() decorator to tell Flask what URL should trigger the function.
methods specify which HTTP methods are allowed. The default is ['GET']

if __name__ == '__main__' → __name__ is a special variable in Python which takes the value of the script name. This line ensures that our Flask app runs only when it is executed in the main file and not when it is imported into some other file

app.run(host='0.0.0.0', port=1028) → Run the Flask application

host specifies the server on which we want our flask application to run. The default value for host is localhost or 127.0.0.1

0.0.0.0 means all IPv4 addresses on the local machine. This ensures that the server will be reachable from all addresses.
The default port value is 1208 and you can set the parameter port to use the port number of your choice.

< Create an border of registration form in python using tkinter Last >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top