from flask import Flask
# If `entrypoint` is not defined in app.yaml,
App Engine will look for an app
# called `app` in `main.py`.
app = Flask(__name__)
@app.route('/', methods=['GET'])
def hello():
"""Return a friendly HTTP greeting."""
return 'Hello UCONN from John Iacovacci!\n'
if __name__ == '__main__':
# 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.
app.run(host='localhost', port=8080, debug=True)
main__ — Top-level script environment
https://docs.python.org/3/library/__main__.html#module-__main__
'__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.
A module can discover whether or not it is running in the main
scope by checking its own __name__, which allows a common idiom for conditionally executing code
in a module when it is run as a script or with python -m but not when it is imported:
if __name__ == "__main__":
# execute only if run as a script
main()
For a package, the same effect can be achieved by including a__main__.py module, the contents of which will be executed when the
module is run with -m.
__name__ is a built-in variable which evaluates to the name
of the current module. Thus it can be 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.
Consider two separate files File1 and File2.
Flask
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.
It began as a simple wrapper around Werkzeug and Jinja
and has become one of the most popular Python web
application frameworks.
Flask offers suggestions, but doesn't enforce any
dependencies or project layout. It is up to the developer
to choose the tools and libraries they want to use.
There are many extensions provided by the community that
make adding new functionality easy.
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. Different
methods of data retrieval from specified URL are defined in this protocol.
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
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
Host: w3schools.com
name1=value1&name2=value2
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
Web applications use different HTTP methods when accessing
URLs. You should familiarize yourself with the HTTP methods
as you work with Flask. 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)
In computer networking, 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.txtfile 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.
You configure your App Engine app's settings in the app.yaml
file. The app.yaml file also contains information about your app's
code, Node.js runtime, and environment variables.
$ 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¶
Generating HTML from within Python is not fun, and actually pretty cumbersome because you have to do the HTML escaping on your own to keep the
application secure. Because of that Flask configures the Jinja2 template engine for you automatically.
To render a template you can use the render_template() method. All you have to do is provide the name of the template
and the variables you want to pass to the template engine as
keyword arguments. Here’s a simple example of how to render a
template:
The Request Context¶
The request context keeps track of the request-level data during a request. Rather than passing the request object to each function that runs
during a request, the request and session proxies are accessed instead.
This is similar to the The Application Context
, which keeps track of the application-level data independent of a request. A corresponding application context is pushed when a request context
is pushed.
https://codelabs.developers.google.com/codelabs/cloud-app-engine-python3/#0
No comments:
Post a Comment