简体   繁体   English

使用 JavaScript 触发 Python 脚本

[英]Trigger A Python Script By Using JavaScript

I am doing a little project where i have prepared the back end in python and the graphic user interface woth HTML, CSS & JS.我正在做一个小项目,我在 python 中准备了后端和 HTML、CSS 和 JS 的图形用户界面。

The python script doesn't require any external librariesand the only thing that it does is opening a JSON file with the data processed, since it's a game the results are casually generated, and because of this I don't have to pass any parameters to the script. python脚本不需要任何外部库,它唯一要做的就是打开一个处理数据的JSON文件,因为它是一个游戏,结果是随便生成的,因此我不必传递任何参数给剧本。 The problem is that I don't know how to trigger the script so it generates the json that i can access trough JabaScript.问题是我不知道如何触发脚本,所以它会生成我可以通过 JabaScript 访问的 json。

the scheme of the project is this: index.html该项目的方案是这样的:index.html

pages (folder)页(文件夹)

  • newGame.html新游戏.html
  • loadGame.html加载游戏.html
  • rules.html规则.html

python (folder)蟒蛇(文件夹)

  • python_script1.py python_script1.py
  • python_script2.py python_script2.py
  • python_script3.py python_script3.py
  • main.py主文件

specifically i have to trigger the script once the user has loaded the newGame page or the loadGame one.具体来说,一旦用户加载了 newGame 页面或 loadGame 页面,我就必须触发脚本。

(obiuvsly the js isn't node.js is actual client-side JavaScript) (显然 js 不是 node.js 是实际的客户端 JavaScript)

I obiuvsly did some research and i found the pyodyde open source project does what i want, the fact is that i can't figure out how to connect the interface file with the back-end one with this resource.我obiuvsly做了一些研究,我发现pyodyde开源项目做了我想要的,事实是我不知道如何用这个资源将接口文件与后端文件连接起来。

solution解决方案

Ok, with some other research i found out how to solve my problem.好的,通过其他一些研究,我发现了如何解决我的问题。 But firstable I want to thank you guys for recommending me the PyScript library.但首先我要感谢你们向我推荐 PyScript 库。

The problem I was having is that i needed to connect a python file within the pyscript tag.我遇到的问题是我需要在 pyscript 标签中连接一个 python 文件。 At first i tought that I had to import it with the import keyword and than executing the file.起初我认为我必须使用import关键字导入它而不是执行文件。 But than I discovered that the py-script tag has an src attribute and you can link all the external files that you want.但是我发现 py-script 标签有一个src属性,你可以链接所有你想要的外部文件。

So, in the end, iall i had to do was to connect my file is this:所以,最后,我所要做的就是连接我的文件是这样的:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>newGame</title>
  <link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
  <py-script src= "./python scripts/test_file.py"></py-script>
  <script src="script.js"></script>
</body>
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</html>

There is a new library called PyScript (it can help you run python in your HTML), all you need to do to install the cli is doing this command: pip install pyscript-cli (This command will install the cli not the library).有一个名为 PyScript 的新库(它可以帮助您在 HTML 中运行 python),安装 cli 所需要做的就是执行以下命令: pip install pyscript-cli (此命令将安装 cli 而不是库)。 then you should do this command: pyscript wrap python_file.py (NOTE: this will convert the python file to an html file so the python file name would be the name of the html file) And it should be done.那么你应该执行以下命令: pyscript wrap python_file.py (注意:这会将 python 文件转换为 html 文件,因此 python 文件名将是 html 文件的名称)并且应该完成。 The HTML code would be this: HTML 代码是这样的:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Homepage</title>
    <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css"/>
    <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</head>
<body>
<py-script>
Code here
</py-script>
</body>
</html>

但是,一旦我输入命令并从 python 脚本创建 html 文件,如何将它连接到“newLevel”页面?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM