Giter Site home page Giter Site logo

python-worker-hello-world's Introduction

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

  • Clone repository (git clone https://github.com/cloudflare/python-worker-hello-world)
  • Run npm install
  • Update wrangler.toml with your project name, account_id, and route as required

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' }
    })))

python-worker-hello-world's People

Contributors

cherry avatar dependabot[bot] avatar jbampton avatar tjmcewan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-worker-hello-world's Issues

Examples don't work

I'm trying to get started with Python workers, but the fact that the example doesn't work is not very encouraging:

23:06:53 $ wrangler generate my-python-project https://github.com/cloudflare/python-worker-hello-world

✘ [ERROR] Deprecation:

  `wrangler generate` has been deprecated.
  Try running `wrangler init` to generate a basic Worker, or cloning the template repository
  instead:
  
  git clone https://github.com/cloudflare/python-worker-hello-world
  
  Please refer to https://developers.cloudflare.com/workers/wrangler/deprecations/#generate for more
  information.


If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose

stavros@zeus:/tmp 
23:06:54 $ git clone https://github.com/cloudflare/python-worker-hello-world
Cloning into 'python-worker-hello-world'...
Resolving deltas: 100% (39/39), done.

stavros@zeus:/tmp 
23:07:03 $ cd python-worker-hello-world/

stavros@zeus:/t/python-worker-hello-world (master|✔) 
23:07:10 $ wrangler publish
 ⛅️ wrangler 2.0.12 
--------------------
▲ [WARNING] Processing wrangler.toml configuration:

    - 😶 Ignored: "type":
      Most common features now work out of the box with wrangler, including modules, jsx,
  typescript, etc. If you need anything more, use a custom build.
    - 😶 Ignored: "webpack_config":
      Most common features now work out of the box with wrangler, including modules, jsx,
  typescript, etc. If you need anything more, use a custom build.
    - Deprecation: "zone_id":
      This is unnecessary since we can deduce this from routes directly.



✘ [ERROR] Processing wrangler.toml configuration:

    - Expected "route" to be either a string, or an object with shape { pattern, custom_domain,
  zone_id | zone_name }, but got "".


If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose

Python Libraries supported?

Hey, I just explored this and the first impressions reflect that this thing uses Transcrypt to change python code to JavaScript and run it's equivalent code on workers.
If I'm not wrong, In this way adding support of all python libraries is somewhat impossible.

Is there any way to support them as of now?

Pre Build Issue

In the instructions before pre build, there is
source env/bin/activate

But, what is source?

I get this error when I type it in my command line:

source : The term 'source' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, 
verify that the path is correct and try again.
At line:1 char:1
+ source env/bin/activate
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (source:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Module parse failed: Unexpected token (5:22)

Running on Windows 10. Performed the edits mentioned in issue #2

PS C:\Users\tarun\python-worker-hello-world> wrangler dev
👀  ./__target__/index.js 5:22
Module parse failed: Unexpected token (5:22)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| var __name__ = '__main__';
| export var handleRequest = function (request) {
>       return new Response (, dict ([[, dict ([[, ]])]]));
| };
| addEventListener (, (function __lambda__ (event) {
 @ ./index.py 1:0-38 1:0-38
Error: webpack returned an error. Try configuring `entry` in your webpack config relative to the current working directory, or setting `context = __dirname` in your webpack config.

Wrangler publish fails on Windows 10 due to incorrect python command

I've installed virtualenv, and transcrypt within that env, however, wrangler publish seems to be running a command that fails

⬇️ Installing wranglerjs...
⬇️ Installing wasm-pack...
Python was not found but can be installed from the Microsoft Store: https://go.microsoft.com/fwlink?linkID=2082640Error compiling C:\Users\admin\wranglers\pythonhelloworld\index.py:
 ./index.py
Module build failed (from ./node_modules/transcrypt-loader/__target_es5__/index.js):
Error: Command failed: python3 -m transcrypt --nomin --map --verbose "index"
Python was not found but can be installed from the Microsoft Store: https://go.microsoft.com/fwlink?linkID=2082640
    at checkExecSyncError (child_process.js:611:11)
    at Object.execSync (child_process.js:647:15)
    at Object.main (C:\Users\admin\wranglers\pythonhelloworld\node_modules\transcrypt-loader\__target_es5__\index.js:56:67)
Error: webpack returned an error. Try configuring `entry` in your webpack config relative to the current working directory, or setting `context = __dirname` in your webpack config.

On Windows 10, it's normal for python3 to be ran as python, when it's installed from the installer from https://www.python.org/downloads/

How can I force wrangler to use python instead of python3?

Django on Cloudflare 2023

Do you have any plans for Django on Cloudflare?

1.Cloudflare native build of Django
2.Integrated with D1
3.to use Cloudflare services extensively

Python Libraries on Worker

I see in #3 that purely python can be used. But, I keep getting an error that the libraries are missing.

Error while compiling (offending file last):
        File '...location/app-workers/index.py', line 13, at import of:
        File 'separate_pythonfile.py', line 13, namely:

        Import error, can't find any of:
                Drive:/...location/app-workers/scipy/interpolate/interp2d.py
                Drive:/...location/app-workers/scipy/interpolate/interp2d.js

Any pointers on how I can use libraries on the worker will be appreciated.

Wrangler publish fails on Windows 10

I managed to bypass #1 by deleting the fake Windows 10 python3 (that redirects to Windows Store), and making a copy of python.exe as python3.exe (dirty hack)

However, wrangler publish seems to be failing still

C:\Users\admin\wranglers\pythonhelloworld>wrangler publish
⬇️ Installing wranglerjs...
⬇️ Installing wasm-pack...
 ./index.py
Module not found: Error: Can't resolve './__target__index.js' in 'C:\Users\admin\wranglers\pythonhelloworld'
resolve './__target__index.js' in 'C:\Users\admin\wranglers\pythonhelloworld'
  using description file: C:\Users\admin\wranglers\pythonhelloworld\package.json (relative path: .)
    Field 'browser' doesn't contain a valid alias configuration
    using description file: C:\Users\admin\wranglers\pythonhelloworld\package.json (relative path: ./__target__index.js)
      no extension
        Field 'browser' doesn't contain a valid alias configuration
        C:\Users\admin\wranglers\pythonhelloworld\__target__index.js doesn't exist
      .wasm
        Field 'browser' doesn't contain a valid alias configuration
        C:\Users\admin\wranglers\pythonhelloworld\__target__index.js.wasm doesn't exist
      .mjs
        Field 'browser' doesn't contain a valid alias configuration
        C:\Users\admin\wranglers\pythonhelloworld\__target__index.js.mjs doesn't exist
      .js
        Field 'browser' doesn't contain a valid alias configuration
        C:\Users\admin\wranglers\pythonhelloworld\__target__index.js.js doesn't exist
      .json
        Field 'browser' doesn't contain a valid alias configuration
        C:\Users\admin\wranglers\pythonhelloworld\__target__index.js.json doesn't exist
      as directory
        C:\Users\admin\wranglers\pythonhelloworld\__target__index.js doesn't exist
[C:\Users\admin\wranglers\pythonhelloworld\__target__index.js]
[C:\Users\admin\wranglers\pythonhelloworld\__target__index.js.wasm]
[C:\Users\admin\wranglers\pythonhelloworld\__target__index.js.mjs]
[C:\Users\admin\wranglers\pythonhelloworld\__target__index.js.js]
[C:\Users\admin\wranglers\pythonhelloworld\__target__index.js.json]
 @ ./index.py 1:0-38 1:0-38
Error: webpack returned an error. Try configuring `entry` in your webpack config relative to the current working directory, or setting `context = __dirname` in your webpack config.

Workers KV example

Their is an example in the readme for getting a value from a workers KV kew, but it is not clear on how the response is used. Can the example either be documented on what it is doing, or can a functional example be added that can be easily tested? I will gladly add clarification if needed.

FastAPI on Cloudflare

Thanks for the Python library! Is there any plan to support the FastAPI framework?

python 3.8

Is it impossible to use python 3.8+?

Hello, @jbampton
I just want to join with you on discord.
Could you share the invitation link here?
Regards

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.