0

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.

martineau
124k29 gold badges181 silver badges319 bronze badges
asked May 14, 2022 at 20:16
1
  • I recommend using flask for this, i.e. write an actual backend / web server. This way you can use fetch() to request arbitrary data from the server at any point. Commented May 14, 2022 at 20:39

3 Answers 3

1

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>
answered May 15, 2022 at 20:06
Sign up to request clarification or add additional context in comments.

1 Comment

P.S. is it better to put links to external packages (stylesheets/sciripts ecc...) in the head tag or at the end before the closing of the html tag?
0

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>
answered May 14, 2022 at 21:00

Comments

0

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?

answered May 15, 2022 at 6:47

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.