首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Pyscript输入和输出文本?

如何使用Pyscript输入和输出文本?
EN

Stack Overflow用户
提问于 2022-05-02 23:29:56
回答 2查看 5.9K关注 0票数 3

我正在学习Python,您可以在HTML5文件中使用HTML5编写Python。作为python编码器,我想尝试一下web开发,同时仍然使用python,所以如果我们可以使用py脚本输出和输入信息,那将是很有帮助的。

例如,有人能解释一下如何使这个函数工作:

代码语言:javascript
复制
<html>
     <head>
          <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
          <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
     </head>
     <body>
          <div>Type an sample input here</div>
          <input id = “test_input”></input>
          <-- How would you get this button to display the text you typed into the input into the div with the id, “test”--!>
          <button id = “submit-button” onClick = “py-script-function”>
          <div id = “test”></div>
          <div 
<py-script>

<py-script>
     </body>
</html

我很感激它,我希望这也能帮助其他py脚本用户。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-05-03 01:13:14

我检查了源代码 on GitHub并找到了文件夹示例

我使用文件todo.htmltodo.py创建了这个index.html

(我使用本地服务器python -m http.server进行了测试)

我发现了一些元素,因为我对JavaScript和CSS有一定的经验,所以学习JavaScript和CSS使用JavaScript元素可能是件好事。

index.html

代码语言:javascript
复制
<!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>
  <div>Type an sample input here</div>
  <input type="text" id="test-input"/>
  <button id="submit-button" type="submit" pys-onClick="my_function">OK</button>
  <div id="test-output"></div>

<py-script>
from js import console

def my_function(*args, **kwargs):

    #print('args:', args)
    #print('kwargs:', kwargs)

    console.log(f'args: {args}')
    console.log(f'kwargs: {kwargs}')
    
    text = Element('test-input').element.value

    #print('text:', text)
    console.log(f'text: {text}')

    Element('test-output').element.innerText = text
</py-script>

  </body>
</html>

这里用JavaScript控制台在火狐的DevTool中截图。

加载所有模块需要更长的时间。

(从Create pyodine runtimeCollecting nodes...)

接下来,您可以看到来自console.log()的输出。

您也可以使用print(),但它会显示带有额外错误writing to undefined ...的文本。

票数 7
EN

Stack Overflow用户

发布于 2022-05-06 21:24:42

显示输出的另一种方法是替换

代码语言:javascript
复制
Element('test-output').element.innerText = text

通过

代码语言:javascript
复制
pyscript.write('test-output', text)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72093397

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档