I am running a very basic pyscript code that accesses a python file.
The following is the HTML file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PyScript Debug</title>
<!-- Using the latest stable version of PyScript -->
<link rel="stylesheet" href="https://pyscript.net/releases/2024.9.1/core.css">
<script type="module" src="https://pyscript.net/releases/2024.9.1/core.js"></script>
</head>
<body>
<h1>PyScript Debugging Example</h1>
<!-- Terminal display -->
<py-terminal></py-terminal>
<!-- Load the main.py file from the server -->
<script src="https:/vivliosyn.com/python/main_1.py" config ="https:/vivliosyn.com/python/pyscript.json" ></script>
</body>
</html>
”’
The python file – main_1.py – has a single line
”’
print ("1")
”’
Both the HTML and the Python file, as well as the pyscript.json
file are store in the same folder on a server (vivliosyn.com
/Python).
However, I am not getting the Python file output. I am able to get the output if I directly add the Python code directly to the HTML file.
However, I am trying to build an application that has to access a larger python file that is saved as a .py
file.
5
In PyScript, when you want to load and run an external Python file from a server, instead of using the tag, you need to use specific PyScript tags. You can use the tag to directly run Python code in the HTML, and also use other tags to load external Python files.
To load an external Python file, you can use the src attribute inside the py-script
<py-script src="https://vivliosyn.com/python/main_1.py"></py-script>
8
Pyscript always looks for the source within the project.It can be either a folder or the file and we mention those details in the .toml file or .json file within config tags. For security reasons external source is blocked.