cloudflare / python-worker-hello-world

Python hello world for Cloudflare Workers
Apache License 2.0
275 stars 49 forks source link

Python hello world for Cloudflare Workers

Your Python code in index.py, running on Cloudflare Workers.

In addition to Wrangler and npm, you will need to install Transcrypt, including Python 3.7 and virtualenv.

Wrangler

Further documentation for Wrangler can be found here.

Transcrypt

Before building your project, you'll need to do one-time setup of Transcrypt. Assuming you have Python 3.7 and virtualenv installed per the linked instructions above, that setup on unix systems looks like the following (for windows see virtualenv docs):

cd projectname

virtualenv env

source env/bin/activate

pip install transcrypt

After that you can run Wrangler commands, such as wrangler publish to push your code to Cloudflare. If you exit virtualenv (deactivate) and return to the project directory later, you'll need to activate virtualenv (source env/bin/activate) but will not need to rerun the other installation commands.

If python3 is not Python 3.7 on your system, make sure you install it, create the virtualenv using the right version of Python, and edit webpack.config.js under command to specify the correct path to the Python 3.7 executable in the virtualenv directory. If you are using Windows, see this workaround for an issue with transcrypt-loader paths.

For more information on how Python translates to Javascript, see the Transcrypt docs. especially the module mechanism and aliases.

Because of aliases, for a KV namespace binding named KV you can use KV.put normally, but need to use KV.js_get instead of KV.get. For example, a handler using KV might look like:

def handleRequest(request):
    return KV.js_get('foo').then(
        lambda v: __new__(Response('Python Worker hello world! ' + v, {
        'headers' : { 'content-type' : 'text/plain' }
    })))