Giter VIP home page Giter VIP logo

serverless-functions-python's Introduction

Serverless Functions Python ๐Ÿ’œ

PyPI version pre-commit.ci status pre-commit.ci status

Scaleway Serverless Functions Python is a framework that simplifies Scaleway Serverless Functions local development. It enables you to debug your function locally and provide the event data format used in Scaleway Serverless Functions.

This library helps you to write functions but for deployment please refer to the documentation.

Get started with Scaleway Functions:

Testing frameworks for Scaleway Serverless Functions in other languages can be found here:

โš™๏ธ Quickstart

You can use pip to install the framework:

pip install scaleway-functions-python
# handler.py

# Standard entrypoint to a Scaleway serverless function
def handler(event, context):
    if event["httpMethod"] != "GET":
         return {"statusCode": 405, "body": "Invalid method!"}
    return "Hello World!"

if __name__ == "__main__":
    # The import is conditional so that you do not need
    # to package the library when deploying on Scaleway Functions.
    from scaleway_functions_python import local
    local.serve_handler(handler, port=8080)

You can then run your function locally:

$ python handler.py
$ curl http://localhost:8080
> Hello World!
$ curl -X POST http://localhost:8080
> Invalid method!

๐Ÿš€ Features

This repository aims to provide a better experience on local testing, utils, and documentation

๐Ÿก Local testing

What this package does:

  • Format Input: Serverless Functions have a specific input format encapsulating the body received by functions to add some useful data. The local testing package lets you interact with the formatted data.
  • Advanced debugging: To improve developer experience you can run your handler locally and debug it by running your code step-by-step or reading output directly before deploying it.

What this package does not:

  • Simulate performance: Scaleway FaaS lets you choose different options for CPU/RAM that can have an impact on your development. This package does not provide specific limits for your function on local testing but you can profile your application or you can use our metrics available in Scaleway Console to monitor your application.
  • Deploy functions: When your function is uploaded we package it in an environment that can be different than yours. Our build pipelines support several dependencies but sometimes require specific system dependencies (especially those related to lib c) that we don't support If you have compatibility issues, please see the help section.

๐Ÿงฑ Type hints

The framework provides some types hints to make it easier to develop your handler. See this example for more information on how to use them.

โ“ FAQ

Why do I need an additional package to call my function?

Your Function Handler can be served by a simple HTTP server but Serverless Ecosystem involves a lot of different layers that will change changes the headers, input and output of your function. This package aims to simulate everything your request will go through to help you debug your application properly. This library is not mandatory to use Scaleway Serverless Functions.

How my function will be deployed

To deploy your function please refer to our official documentation.

Do I need to deploy my function differently?

No, this framework does not affect deployment or performance.

How can I use my packaged dependencies?

When deploying Python functions, your dependencies must be bundled in a package folder at the root of your project. For local testing, you can set PYTHONPATH=$(pwd)/package to make your dependencies available. This can be useful to avoid packaging your dependencies in multiple locations.

Please note that this does not work for native dependencies as the Scaleway Python runtime is different from your local machine.

Why are my logs not showing up when using the print function?

By default, stdout is buffered in Python, so calling print without flush=True can lead to missing logs when running locally. If you experience this issue, you can export the environment variable PYTHONUNBUFFERED with export PYTHONUNBUFFERED=1. This will flush stdout on every print call.

๐ŸŽ“ Contributing

We welcome all contributions to our open-source projects, please see our contributing guidelines.

Do not hesitate to raise issues and pull requests we will have a look at them.

๐Ÿ“ญ Reach Us

We love feedback. Feel free to:

serverless-functions-python's People

Contributors

bemilie avatar cyclimse avatar dependabot[bot] avatar lioman avatar lucasscw avatar pre-commit-ci[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

serverless-functions-python's Issues

Internal Server Error 500 on POST requests containing non-ASCII characters (multipart/form-data with a picture)

Community Note

  • Please vote on this issue by adding a ๐Ÿ‘ reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

What did you do?

Apply a slight modification to the example from README.md under Quickstart, so it would accept POST requests. Then start the test server and post a multipart/form-data including a picture, with curl:

curl -v -F filename=picture.jpg -F upload=@/full/path/to/picture.jpg http://localhost:8080

What did you expect to see?

"Hello World!"

What did you see instead?

*   Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.81.0
> Accept: */*
> Content-Length: 2175522
> Content-Type: multipart/form-data; boundary=------------------------c892a83a67802793
> Expect: 100-continue
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 100 Continue
* Mark bundle as not supporting multiuse
< HTTP/1.1 100 Continue
* We are completely uploaded and fine
* Mark bundle as not supporting multiuse
< HTTP/1.1 500 INTERNAL SERVER ERROR
< Server: Werkzeug/3.0.0 Python/3.10.12
< Date: Wed, 11 Oct 2023 09:11:02 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 21974
< Connection: close

[...]

ValueError: string argument should contain only ASCII characters // Werkzeug Debugger

[...]

Traceback:
File "/usr/lib/python3.10/base64.py", line 37, in _bytes_from_decode_data

[...]

During handling of the above exception, another exception occurred:
File "~/.local/lib/python3.10/site-packages/flask/app.py, line 2213, in __call__

[...]

The WSGI server calls the Flask application object as the WSGI application. This calls :meth:`wsgi_app`, which can be wrapped to apply middleware.

[...]

What version of Python are you using (python --version)?

Python 3.10.12

Does this issue reproduce with the latest release?

Yes

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.