Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(functions): fix Python dependencies example #73

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 34 additions & 28 deletions functions/python-dependencies/README.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,57 @@
# Using Python Requirements with Serverless Framework

If you need to include a PyPI package with your function, you will need to vendor it when packaging your function. This can be done by using `pip install --target package ...` before deploying.
If you need to install dependencies to run your Python function, you will need to include it in the zip file packaged with your function.

Here's a simple example to achieve that with Serverless Framework.
This can be done easilye with the [`serverless-python-requirements` plugin](https://github.com/serverless/serverless-python-requirements) for Serverless Framework.

## Testing with serverless offline for Python
## Requirements

In order to test your function locally before deployment in a serverless function, you can install our offline testing library with:
This example assumes that you are familiar with how Serverless Functions work. If needed, you can check [Scaleway official documentation](https://www.scaleway.com/en/docs/serverless/functions/quickstart/).

```bash
pip install -r requirements-dev.txt
```
This example uses the Scaleway Serverless Framework Plugin. Please set up your environment as described in the [Scaleway Serverless Framework Plugin repo](https://github.com/scaleway/serverless-scaleway-functions) before trying out the example.

Import your environment variables using:
> [!NOTE]
> You must also have the same version of Python used in your function available locally on your system, i.e. if you are using the function runtime `python310`, you must have the command `python3.10` available in your shell.

```bash
export database_model="english-corpus.sqlite3"
```
## Running on Scaleway

Launch your function locally:
Run the following to deploy your function:

```console
# Install node dependencies
npm install

```bash
python python-dependencies.py
# Deploy
serverless deploy
```

Test your local function using `curl`:
The deploy command should print the URL of the new function.

```bash
curl -i -X POST localhost:8080 -d '{"message":"Hello"}'
You can then use `curl` to check that it works, and should see the response:

```raw
Response status: 200
```

## Setup
You can also view your function in the [Scaleway Console]().

First, you need to set up the [Serverless Framework](https://www.serverless.com/framework/docs/getting-started).
## Testing with serverless offline for Python

Then, you can run:
In order to test your function locally before deployment in a serverless function, you can use our offline testing library with:

```bash
# Install node dependencies
npm install
```console
pip install -r requirements-dev.txt
```

# Deploy
./bin/deploy.sh
Launch your function locally:

```console
python3 handler.py
```

The deploy command should print the URL of the new function. You can then use `curl` to check that it works, and should see the response:
Test your local function using `curl` (in another terminal)

```raw
Response status: 200
```console
curl localhost:8080
```

7 changes: 0 additions & 7 deletions functions/python-dependencies/bin/deploy.sh

This file was deleted.

5 changes: 0 additions & 5 deletions functions/python-dependencies/bin/deps.sh

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def handle(event, context):
},
}


if __name__ == "__main__":
from scaleway_functions_python import local

local.serve_handler(handle)
Loading
Loading