# Create and deploy an HTTP Cloud Run function by using Python (1st gen)

This guide takes you through the process of writing a Cloud Run function
using the Python runtime. There are two types of Cloud Run functions:

- An HTTP function, which you invoke from standard HTTP requests.
- An event-driven function, which you use to handle events from your Cloud infrastructure, such as messages on a Pub/Sub topic, or changes in a Cloud Storage bucket.

The sample shows how to create a simple HTTP function.

> [!IMPORTANT]
> **Learn more** : For more details, read about [HTTP functions](https://docs.cloud.google.com/functions/1stgendocs/writing/write-http-functions.md.txt) and [event-driven functions](https://docs.cloud.google.com/functions/1stgendocs/writing/write-event-driven-functions.md.txt).

## Before you begin

1. Prepare your development environment.
   [Go to the Python setup guide](https://docs.cloud.google.com/python/docs/setup.md.txt)

<br />

## Create a function

1. Create a directory on your local system for the function code:

   ### Linux or Mac OS X

   ```python
   mkdir ~/helloworld
   cd ~/helloworld
   ```

   ### Windows

   ```python
   mkdir %HOMEPATH%\helloworld
   cd %HOMEPATH%\helloworld
   ```
2. Create a `main.py` file in the `helloworld` directory with the
   following contents:

   ```python

   import functions_framework


   from markupsafe import escape

   @functions_framework.http
   def hello_http(request):
       """HTTP Cloud Function.
       Args:
           request (flask.Request): The request object.
           <https://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data>
       Returns:
           The response text, or any set of values that can be turned into a
           Response object using `make_response`
           <https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response>.
       """
       request_json = request.get_json(silent=True)
       request_args = request.args

       if request_json and "name" in request_json:
           name = request_json["name"]
       elif request_args and "name" in request_args:
           name = request_args["name"]
       else:
           name = "World"
       return f"Hello {escape(name)}!"

   ```

   This example function takes a name supplied in the HTTP request and returns
   a greeting, or "Hello World!" when no name is supplied.

   > [!NOTE]
   > **Note:** Cloud Run functions looks for deployable functions in `main.py` by default. Use the [`--source`](https://docs.cloud.google.com/sdk/gcloud/reference/functions/deploy.md.txt#--source) flag when [deploying your function via `gcloud`](https://docs.cloud.google.com/functions/1stgendocs/create-deploy-http-python-1st-gen.md.txt#deploying_the_function) to specify a different directory containing a `main.py` file.

## Specify dependencies

Dependencies in Python are managed with [pip](https://pip.pypa.io/en/stable/)
and expressed in a metadata file called
[`requirements.txt`](https://pip.pypa.io/en/stable/user_guide/#requirements-files).
This file must be in the same directory as the `main.py` file that contains
your function code.

You don't need to create a `requirements.txt` to run this particular sample,
but suppose you wanted to add your own dependencies. Here's how you would do it:

1. Create a `requirements.txt` file in the `helloworld` directory.

2. Add the function's dependency, to your `requirements.txt` file, for example:

   ```python
   # An example requirements file, add your dependencies below
   sampleproject==2.0.0
   ```

> [!IMPORTANT]
> **Learn more** : For more details, read about [specifying dependencies](https://docs.cloud.google.com/functions/1stgendocs/writing/specifying-dependencies-python.md.txt).

## Deploy the function

To deploy the function with an HTTP trigger, run the following
command in the `helloworld` directory:

```sh
gcloud functions deploy hello_http --no-gen2 --runtime python314 --trigger-http --allow-unauthenticated
```

The `--allow-unauthenticated` flag lets you reach the function
[without authentication](https://docs.cloud.google.com/functions/1stgendocs/securing/managing-access-iam.md.txt#allowing_unauthenticated_http_function_invocation).
To require
[authentication](https://docs.cloud.google.com/functions/1stgendocs/securing/authenticating.md.txt#developers), omit the
flag.

> [!IMPORTANT]
> **Learn more** : For more details, read about [deploying Cloud Run functions](https://docs.cloud.google.com/functions/1stgendocs/deploy.md.txt#basics).

## Test the function

1. When the function finishes deploying, take note of the `httpsTrigger.url`
   property or find it using the following command:

   ```python
   gcloud functions describe hello_http
   ```

   It should look like this:

   ```
   https://GCP_REGION-PROJECT_ID.cloudfunctions.net/hello_http
   ```
2. Visit this URL in your browser. You should see a "Hello World!" message.

   Try passing a name in the HTTP request, for example by using the following
   URL:

   ```
   https://GCP_REGION-PROJECT_ID.cloudfunctions.net/hello_http?name=NAME
   ```

   You should see the message "Hello `NAME`!"

## View logs

Logs for Cloud Run functions are viewable using the Google Cloud CLI, and in the
Cloud Logging UI.

### Use the command-line tool

To view logs for your function with the gcloud CLI, use the
[`logs read`](https://docs.cloud.google.com/sdk/gcloud/reference/functions/logs/read) command, followed by
the name of the function:

```bash
gcloud functions logs read hello_http
```

The output should resemble the following:

```bash
LEVEL  NAME        EXECUTION_ID  TIME_UTC                 LOG
D      hello_http  pdb5ys2t022n  2019-09-18 23:29:09.791  Function execution started
D      hello_http  pdb5ys2t022n  2019-09-18 23:29:09.798  Function execution took 7 ms, finished with status code: 200
```

> [!NOTE]
> **Note:** There is typically a slight delay between when log entries are created and when they show up in Cloud Logging.

### Use the Logging dashboard

You can also view logs for Cloud Run functions from the
[Google Cloud console](https://console.cloud.google.com/project/_/logs?service=cloudfunctions.googleapis.com).

> [!IMPORTANT]
> **Learn more** : For more details, read about [writing, viewing, and responding to logs](https://docs.cloud.google.com/functions/1stgendocs/monitoring/logging.md.txt).