In pyodide, it is not support the requests module so to fetch the data from api we used open_url and how can we use the api for post the Data using pyodide
Update: Pyodide v0.21.2
It is currently impossible to use the requests since sockets are not available in Pyodide.
However, you can use JS Fetch API direct form Python code. To do this, you must import window
object from js
module. Here is a live demo:
(async () => { // enable await
// init pyodide
let pyodide = await loadPyodide();
// import window object
pyodide.runPython('from js import window')
// fetch json
let result = await pyodide.runPythonAsync("window.fetch('http://karay.me/truepyxel/test.json')")
// result is now a response object
console.log(result)
// convert response to json
let data = await result.json()
console.log(data)
alert(data.msg)
})() // call the function immediately
<script src="https://cdn.jsdelivr.net/pyodide/v0.21.2/full/pyodide.js"></script>
More details here.
Pyodide v0.15.0
let python_code = `
from js import window
def fetch():
window.fetch('http://karay.me/truepyxel/test.json').then(lambda resp: resp.json()).then(lambda jsoh: show_result(jsoh))
def show_result(data):
div = window.document.createElement('div')
#insert into body as a first child
window.document.body.prepend(div)
div.innerHTML=window.JSON.stringify(data)
`
// init environment
languagePluginLoader
// then run Python code
.then(() => pyodide.runPythonAsync(python_code));
<!DOCTYPE html>
<html>
<head>
<script src="https://pyodide-cdn2.iodide.io/v0.15.0/full/pyodide.js"></script>
</head>
<body>
<button onclick='pyodide.globals.fetch()'>Fetch</button>
</body>
</html>
The requests module is currently not supported in pyodide because it relies on sockets which are not implemented in the WebAssembly browser VM.
You can however, make POST calls using Web APIs in pyodide. Below is an example using XMLHttpRequest
from js import XMLHttpRequest, Blob
import json
data = {"a": 1}
req = XMLHttpRequest.new()
req.open("POST", "https://httpbin.org/anything", False)
blob = Blob.new([json.dumps(data)], {type : 'application/json'})
req.send(blob)
str(req.response)
In the future it's possible that some of the classical HTTP client modules might be patched to use Web APIs in pyodide (cf pyodide#140).
pyodide
with pip install pyodide-py
, but that doesn't seem to allow me to import js
. –
Ious As already mentioned we cannot use requests, so you can use javascript fetch to perform the POST operation, the sample snippet will work with pydiode and pyscript (async format), this same code can be written in sync-fashion using add_done_callback
import asyncio
from js import console, fetch
async def post_data():
response = await fetch('http://example.com/test',{'method':'POST'})
json = await response.json()
console.log('json', json)
return json
await post_data()
js
? I don't think it's this one pypi.org/project/javascript ? –
Ious © 2022 - 2024 — McMap. All rights reserved.
js
module? It's not this one I think pypi.org/project/javascript? – Ious