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
def clear_data():
document.getElementById("filename").innerHTML = ''
document.getElementById("filesize").innerHTML = ''
document.getElementById('content').contentDocument.body.innerText = ''
async def file_select(event):
# Note: print() does not work in event handlers
clear_data()
try:
options = {
"multiple": False,
"startIn": "documents"
}
fileHandles = await window.showOpenFilePicker(Object.fromEntries(to_js(options)))
except Exception as e:
console.log('Exception: ' + str(e))
return
for fileHandle in fileHandles:
file = await fileHandle.getFile()
document.getElementById("filename").innerHTML = 'File Name: ' + file.name
document.getElementById("filesize").innerHTML = 'File Size: ' + str(file.size)
document.getElementById("filetype").innerHTML = 'File Type: ' + str(file.type)
document.getElementById("filedate").innerHTML = 'File date: ' + str(file.lastModifiedDate)
content = await file.text()
document.getElementById('content').contentDocument.body.innerText = content
def setup_button():
# Create a Python proxy for the callback function
file_select_proxy = create_proxy(file_select)
# Set the listener to the callback
document.getElementById("file_select").addEventListener("click", file_select_proxy, False)
setup_button()