我试图调用我创建的python函数.但没有任何输出,也没有资源如何实现.

Code :

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</head>
<body>

<p>Click on the "Choose File" button to upload a file:</p>

<form>
  <input  type="file" id="myFile" name="filename">
  <input type="submit" onClick="readfile(filename)" name="SUBMIT">
</form>

<py-script>

def readfile(filename):
    with open(filename) as mfile:
        head = [next(mfile) for x in range(1,5)]
        print(head)

</py-script>

</body>
</html>

如果有人能提供像……这样的信息,那将非常有帮助…

如何将所选文件传递给my function和python将被执行&amp;返回屏幕上的输出.

推荐答案

在浏览器中编写Python时,必须重新考虑操作的执行方式.通常,Python程序是程序化的.基于浏览器的应用程序是异步的.

第一步是启用异步功能:

import asyncio

基于浏览器的程序无法直接访问本地文件系统.你的代码没有做你认为的事情.

def readfile(filename):
    with open(filename) as mfile:
        head = [next(mfile) for x in range(1,5)]
        print(head)

您的代码正在从允许的浏览器虚拟文件系统中读取.您试图从`元素处理事件,但试图访问其他位置.

注意:我不知道"filename"的文件内容是什么,所以我没有在读取文件后加入您的代码.

你的代码无法读取文件.您必须要求浏览器读取应用程序的文件.这是通过FileReader类执行的.

例子:

async def process_file(event):
        # Currently, PyScript print() does not work in this 
        # type of code (async callbacks)
        # use console.log() to debug output

        fileList = event.target.files.to_py()

        for f in fileList:
                data = await f.text()
                document.getElementById("content").innerHTML = data
                # Add your own code to process the "data" variable
                # which contains the content of the selected file

另一个问题是,您正在将Python函数作为回调传递.那是行不通的.相反,必须调用create_proxy()为Python函数创建回调代理.浏览器将调用代理,然后代理将调用Python函数.

例子:

# Create a Python proxy for the callback function
# process_file() is your function to process events from FileReader
file_event = create_proxy(process_file)

# Set the listener to the callback
document.getElementById("myfile").addEventListener("change", file_event, False)

我把这个解决方案的副本放在了我的网站上.你可以右键点击页面下载源代码.

File Example Demo

完整解决方案:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
<title>File Example</title>
</head>
<body>

<p>This example shows how to read a file from the local file system and display its contents</p>
<br />
<p>Warning: Not every file type will display. PyScript removes content with tags such as XML, HTML, PHP, etc. Normal text files will work.</p>
<br />
<p>No content type checking is performed to detect images, executables, etc.</p>
<br />
<label for="myfile">Select a file:</label>
<input type="file" id="myfile" name="myfile">
<br />
<br />
<div id="print_output"></div>
<br />
<p>File Content:</p>
<div style="border:2px inset #AAA;cursor:text;height:120px;overflow:auto;width:600px; resize:both">
  <div id="content">
  </div>
</div>

<py-script output="print_output">
import asyncio
from js import document, FileReader
from pyodide import create_proxy

async def process_file(event):
    fileList = event.target.files.to_py()

    for f in fileList:
        data = await f.text()
        document.getElementById("content").innerHTML = data

def main():
    # Create a Python proxy for the callback function
    # process_file() is your function to process events from FileReader
    file_event = create_proxy(process_file)

    # Set the listener to the callback
    e = document.getElementById("myfile")
    e.addEventListener("change", file_event, False)

main()
</py-script>

</body>
</html>

Python相关问答推荐

对整个 pyramid 进行分组与对 pyramid 列子集进行分组

运行总计基于多列pandas的分组和总和

不理解Value错误:在Python中使用迭代对象设置时必须具有相等的len键和值

Python键入协议默认值

将tdqm与cx.Oracle查询集成

如何将多进程池声明为变量并将其导入到另一个Python文件

索引到 torch 张量,沿轴具有可变长度索引

需要帮助重新调整python fill_between与数据点

Pandas Data Wrangling/Dataframe Assignment

Matplotlib中的字体权重

剪切间隔以添加特定日期

如何在海上配对图中使某些标记周围的黑色边框

如何使用pytest在traceback中找到特定的异常

EST格式的Azure数据库笔记本中的当前时间戳

高效地计算数字数组中三行上三个点之间的Angular

pyspark where子句可以在不存在的列上工作

使用元组扩展字典的产品挑战

极地数据帧:ROLING_SUM向前看

Python键盘模块不会立即检测到按键

使用_in链接操作管道传输的中间结果是否可用于链中的后续函数?