Open wiwichips opened 1 year ago
I think this is a great idea. Current plan, subject to change --
Somebody needing a simple version of fetch in JS can probably use the Python requests library from JS in the meantime.
Here are some instructions for adding new API to JavaScript's global namespace:
For instance, if you wanted to add fetch, you could write a js file in python/pythonmonkey/builtin_modules/fetch.js
python.exec(`
def fetch():
return "Hello, World!"
`)
fetch = python.eval('fetch');
if (!globalThis.fetch) {
globalThis.fetch = fetch
}
exports.fetch = fetch;
And then just require this file in python/pythonmonkey/__init.py
:
10
11 # Load the module by default to expose global APIs
12 require("console")
13 require("base64")
14 require("timers")
+ 15 require("fetch")
16
You'll be able to use npm packages, pip packages and more to help implement any of the features. The example above just demonstrates adding a python function inside a commonjs module to globalThis for JavaScript code. Another approach would be to leverage the XHR implementation and some potential polyfills for fetch using xhr that may exist to implement fetch. Another approach could be to write it in Python or even C++.
PythonMonkey team, let me know if I'm missing anything or got anything wrong here.
PythonMonkey team here.
It's recommended to write a py file in python/pythonmonkey/builtin_modules/fetch.py
if most of your code is implemented in Python.
def fetch():
...
from pythonmonkey import globalThis
if not globalThis['fetch']:
globalThis['fetch'] = fetch
exports['fetch'] = fetch
In python/pythonmonkey/__init__.py
, you can use everything our require
function supports, besides builtin_modules
.
It just makes pythonmonkey
preload the polyfill specified in that module.
More documentation is coming at https://docs.pythonmonkey.io/ (not available yet).
Describe your feature request here.
Add the Fetch API to PythonMonkey as per its spec (or a subset of it)
Work for fetch will include:
fetch
function (https://fetch.spec.whatwg.org/#fetch-method)Request
interfaceResponse
interfaceHeaders
interfaceThis will likely be implemented as a wrapper over Python's requests module.
Using
fetch
from JavaScript code would be very familiar for Web developers using PythonMonkey. It will also provide fetch to Python developers as an alternative promise based request api.Example usage (in Python):
Example usage (in JavaScript):
Code example