This example shows how to call the File System Access APIs. These APIs permit local file system access.
File Contents:
import asyncio
import sys
from js import alert, document, Object, window
from pyodide import create_proxy, to_js
async def file_save(event):
# Note: print() does not work in event handlers
try:
options = {
"startIn": "documents",
"suggestedName": "testfile.txt"
}
fileHandle = await window.showSaveFilePicker(Object.fromEntries(to_js(options)))
except Exception as e:
console.log('Exception: ' + str(e))
return
content = document.getElementById("content").value
file = await fileHandle.createWritable()
await file.write(content)
await file.close()
return
def setup_button():
# Create a Python proxy for the callback function
file_save_proxy = create_proxy(file_save)
# Set the listener to the callback
document.getElementById("file_save").addEventListener("click", file_save_proxy, False)
setup_button()