App Engine Hello World Explained
Flask
Flask is a micro web framework written in Python.
Backend framework plays a crucial role in building robust and efficient web
applications.
from flask import Flask
app = Flask(__name__)
@app.route('/', methods=['GET'])
def hello():
"""Return a friendly HTTP greeting."""
return 'Hello UCONN from John Iacovacci!\n'
if __name__ == '__main__':
app.run(host='localhost', port=8080, debug=True)
# If `entrypoint` is not defined in app.yaml, App Engine
will look for an app
# Used when running locally only. When deploying to Google
App
# Engine, a webserver process such as Gunicorn will serve
the app. This
# can be configured by adding an `entrypoint` to
app.yaml.
'__main__' is the name of the scope in which top-level code executes.
A module’s __name__ is set equal to '__main__' when read from standard input, a script, or from an interactive prompt.
if __name__ == "__main__":
# execute only if run as a script
main()
__name__ is a built-in variable which evaluates to the name of the current module. T
Used to check whether the current script is being run on its own or being imported
somewhere else by combining it with if statement, as shown below.
Flask is a lightweight WSGI web application
framework. It is designed to make getting started quick and easy, with the ability to scale up
to complex applications.
A web framework (WF) or web application framework (WAF) is a software
framework that is designed to support the development of web applications
including web services, web resources, and web APIs.
Python code in one module gains access to the code in another module by the
process of importing it.
The import statement is the most common way of invoking the import machinery.
Routing is the mechanism of mapping the URL directly to the code that creates
the webpage.
Use the route() decorator to bind a function to a URL.
app.route(rule, options)
The rule parameter represents URL binding with the function.
The options is a list of parameters to be forwarded to the underlying Rule object.
Http protocol is the foundation of data communication in world wide web.
What is HTTP?
The Hypertext Transfer Protocol (HTTP) is designed to enable communications
between clients and servers.
HTTP works as a request-response protocol between a client and server.
Example: A client (browser) sends an HTTP request to the server; then the
server returns a response to the client. The response contains status information
about the request and may also contain the requested content.
HTTP Methods
GET
POST
PUT
HEAD
DELETE
PATCH
OPTIONS
The two most common HTTP methods are: GET and POST.
The GET Method
GET is used to request data from a specified resource.
GET is one of the most common HTTP methods.
Note that the query string (name/value pairs) is sent in the URL of a GET request:
/test/demo_form.php?name1=value1&name2=value2
GET - Sends data in unencrypted form to the server. Most common method.
The POST Method
POST is used to send data to a server to create/update a resource.
The data sent to the server with POST is stored in the request body of the HTTP request:
POST /test/demo_form.php HTTP/1.1
POST is one of the most common HTTP methods.
Some other notes on POST requests:
POST requests are never cached
POST requests do not remain in the browser history
POST requests cannot be bookmarked
POST requests have no restrictions on data length
POST - Used to send HTML form data to serve
By default, a route only answers to GET requests. You can use the
methods argument of the route() decorator to handle different HTTP methods.
Finally the run() method of Flask class runs the application on the local development
server.
app.run(host, port, debug, options)
localhost is a hostname that refers to the current computer used to access it. It is used to access
the network services that are running on the host via the loopback network interface.
Port 80 is HTTP. Port 443 is HTTPS. Port 8080 is a non-standard, high number port that is popular as an
alternative port for HTTP servers, most often application servers
requirements.txt
requirements.txt file is used for specifying what python packages are required to run the project
you are looking at.
app.yaml file. This configuration file defines your web app's settings for App Engine.
The app.yaml file also contains information about your app's code
$ gcloud app deploy
Deploy your app to upload and run it on App Engine. When you deploy your apps
, you create versions of those apps and their corresponding services in App Engine
Rendering Templates¶
render a template you can use the render_template() method.
Provide the name of the template and the variables you want to pass to the template
engine as keyword arguments.
The Request Context¶
The request context keeps track of the request-level data during a request.
No comments:
Post a Comment