In this lesson you will learn how a flask web app works. The code we have so far is a typical flask script that builds a simple website. So, we’re going to explain what that code does line by line. Application won't open on mac. This is what we have so far:
In line 1 you are making available the code you need to build web apps with flask.
flask
is the framework here, while Flask
is a Python class datatype. In other words, Flask is the prototype used to create instances of web application or web applications if you want to put it simple.Socketio=SocketIO Instance Flask SocketIO instance (if specified, uses socketio.run instead of app.run for Flask application) Should work on windows/linux/mac with no isssues. Develop your app as you would normally do, add flaskwebgui at the end or for tests.
So, once we import Flask, we need to create an instance of the Flask class for our web app. That’s what line 3 does.
__name__
is a special variable that gets as value the string '__main__'
when you’re executing the script. We’ll go back to that in a moment.In lines 5-7 we are defining a function that returns the string “Hey there!”. https://lionever700.weebly.com/blog/kindle-app-mac-catalina. That function is mapped to the home
‘/’
URL. That means when the user navigates to localhost:5000
, the home function will run and it will return its output on the webpage. If the input to the route method was something else, let’s say ‘/about/’
, the function output would be shown when the user visited localhost:5000/about/
. How practical is that?Then we have the mysterious lines 9 and 10. Something you should know is that Python assigns the name
'__main__'
to the script when the script is executed. If the script is imported from another script, the script keeps it given name (e.g. hello.py). In our case we are executing the script. Therefore, __name__
will be equal to '__main__'
. That means the if conditional statement is satisfied and the app.run() method will be executed. This technique allows the programmer to have control over script’s behavior.![App App](/uploads/1/3/3/9/133948479/822626504.jpg)
Notice also that we are setting the
debug
parameter to true
. That will print out possible Python errors on the web page helping us trace the errors. Mac caffeine app store walmart. However, in a production environment, you would want to set it to False
as to avoid any security issues.So far, so good!
However, deploying a flask application that has been built with your main Python installation can be a bad idea sometimes. Apple mac app store app. Furthermore, it is not advisable to even start creating a web app using your main Python installation. Because you use Python for many other things, there can be a lot going in that installation. Free Bonus: Click here to get access to a free Flask + Python video tutorial that shows you how to build Flask web app, step-by-step. Updates:: Upgraded to Python version 3.8.1 as well as the latest versions of Psycopg2, Flask-SQLAlchemy, and Flask-Migrate. This library allows us to convert a flask application to a desktop one with a few lines of code. From flask import Flask from webui import WebUI app = Flask(name) ui = WebUI(app, debug=True) @app.route('/') def home: return 'Hello world!' , 200 if name 'main': ui.run We have only added three lines of code here: importing the.
Flask App In Class
We have a running website with some content returned by the Python function. As you can imagine, returning plain strings from a function doesn’t take us anywhere far.
Run Flask App Locally
If you want to make something more serious and more visually appealing, you would want to incorporate HTML files along your Python file and have your Python code return HTML pages instead of plain strings.
Flask App In Mac Shortcut
You will do just that in the next lesson.
Flask App Machine Learning
- If your code doesn’t work that’s a good thing - September 26, 2019
- Companies are looking for these Python skills! - September 16, 2019
- How to measure the execution time of a Python script - September 16, 2019