使用JavaScript触发Python脚本

发布于 2025-01-28 20:45:28 字数 658 浏览 7 评论 0原文

我正在进行一个小项目,在Python和图形用户界面Woth HTML,CSS& JS。

Python脚本不需要任何外部库,并且它唯一要做的就是用处理数据打开JSON文件,因为它是游戏,因此结果是随意生成的,因此我不必将任何参数传递给脚本。 问题是我不知道如何触发脚本,因此它可以生成可以访问jabascript的JSON。

该项目的计划是: index.html

页面(文件夹)

  • newgame.html
  • loadgame.html
  • prule.html

python(folder)

  • python_script1.py
  • python_script2.py python_script2.py
  • python_script3.py
  • main.py main.py,

我必须具体触发脚本或poad load load load load load loadgame page newgame页面一。

(obiuvsly js不是node.js是实际的客户端JavaScript)

我obiuvsly进行了一些研究,我发现pyodyde开源项目可以做我想要的,事实是我无法找到如何连接界面用此资源与后端一起文件。

I am doing a little project where i have prepared the back end in python and the graphic user interface woth 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.
The problem is that I don't know how to trigger the script so it generates the json that i can access trough JabaScript.

the scheme of the project is this:
index.html

pages (folder)

  • newGame.html
  • loadGame.html
  • rules.html

python (folder)

  • python_script1.py
  • python_script2.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.

(obiuvsly the js isn't node.js is actual client-side 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

So尛奶瓶 2025-02-04 20:45:28

解决方案

好,在其他一些研究中,我发现了如何解决问题。
但首先,我要感谢你们向我推荐了Pyscript库。

我遇到的问题是我需要在Pyscript标签中连接Python文件。
首先,我必须用import关键字导入它,而不是执行文件。
但是我发现PY-Script标签具有src属性,您可以链接所有想要的外部文件。

因此,最后,我要做的就是连接我的文件是:

<!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>

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.

The problem I was having is that i needed to connect a python file within the pyscript tag.
At first i tought that I had to import it with the import keyword and than executing the file.
But than I discovered that the py-script tag has an src attribute and you can link all the external files that you want.

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>
南薇 2025-02-04 20:45:28

有一个名为Pyscript的新库(它可以帮助您在HTML中运行Python),安装CLI所需要做的就是执行此命令:
pip install pyscript-cli(此命令将安装CLI而不是库)。
然后,您应该执行此命令:
Pyscript Wrap Python_file.py(注意:这将将Python文件转换为HTML文件,因此Python文件名是HTML文件的名称)
而且应该做。
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>

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).
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.
The HTML code would be this:

<!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>
任性一次 2025-02-04 20:45:28

但是,一旦我输入命令并从Python脚本创建了HTML文件,我该如何将其连接到“ Newlevel”页面?

But than, once i typed in the command and created the html file from the python script, how do I connect it to the "newLevel" page?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文