Skip to content

3C0N-MQ3/my_first_gc_function

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

jupyter
jupytext
cell_metadata_filter main_language text_representation
-all
python
extension format_name format_version jupytext_version
.md
markdown
1.3
1.16.1

My first Google Cloud Function

If you have a nice base environment in conda, you can clone it to create a new environment for this project. To do so, open the anaconda powershell prompt and run the following command:

conda create --my-first-gcf --clone base

Then, activate the new environment:

conda activate my-first-gcf

Now, install the required packages:

pip install functions-framework flask

This packages will be managed by the conda environment, so you can deactivate it and activate it whenever you want.

Create a new folder for the project and create a new file called main.py.

mkdir my-first-gcf
cd my-first-gcf
touch main.py

In this new file, write the following code:

# Brief example of how to use the package `functions_framework` from Google
# Cloud Functions to create a simple HTTP function.
import flask
import functions_framework

# The idea is to translate a function without having to write the HTTP server
# or complicated request handling logic.

# class flask.Request(environ, populate_request=True, shallow=False)
# Default request object used by default in Flask. Remembers the matched
# endpoints and view arguments. 
def hello_world(request: flask.Request) -> flask.typing.ResponseReturnValue:
    """Show a `Hello, World!` message in the browser for each request.

    Args:
        request (flask.Request): The request object. Instantiated by Flask 
        when a request is received.

    Returns:
        flask.typing.ResponseReturnValue: The response object. Instantiated by
        Flask to send the response back to the client.
    """
    return "Hello, World!"

To test locally, run the following command in the anaconda powershell prompt:

functions-framework --target hello_world --debug

Then, in a web browser, navigate to http://localhost:port/ to see the output. Each time you refresh the page, is like sending a new request to the server.

Further reading:

About

My first google cloud function.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published