0

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
  • While vivliosyn.com/python/pyscript.json really gets me a JSON, vivliosyn.com/python/main_1.py results in HTTP 404, so the file is not there. Commented Sep 15, 2024 at 13:01
  • if you check on vivliosyn.com/python, you will see all three files are there Commented Sep 15, 2024 at 13:02
  • Yes, later I saw that too, but if you click, it just doesn't get served. It may be something with the web server itself. Perhaps some overzealous security setting preventing it to touch python files, so it won't run unsafe code on the server side. Commented Sep 15, 2024 at 13:06
  • If you have access to the server itself, check its logs and configuration. If you don't have access to it, a simple hack could be trying a neutral extension, like main_1.txt. That may help with this simple test case, but when you will have multiple files, modules, etc, the original problem will have to be sorted out. Commented Sep 15, 2024 at 13:10
  • I have access to the server, but not sure exactly what needs to be checked in the logs. pl guide Commented Sep 15, 2024 at 13:16

2 Answers 2

0

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>
Sign up to request clarification or add additional context in comments.

8 Comments

this is what I am using <script src="https:/vivliosyn.com/python/main_1.py" config ="https:/vivliosyn.com/python/pyscript.json" ></script>
As per my understanding the <py-script> tag is used if the python code is directly used within this tags. However, if an external file has to be accessed, then the <script> tag has to be used. But anyway, i have tried both and there was no output for either
Test PyScript Locally To be sure, try adding your Python code directly inside the HTML file and check if the issue is with PyScript or something else: <py-script> print("1") </py-script> If this code works and you see the output "1", it indicates the issue is with loading the external file.
File Accessibility (CORS Issues): If the external file (main_1.py) is hosted on a different domain (vivliosyn.com), you may run into CORS (Cross-Origin Resource Sharing) issues, where your browser blocks the request to fetch the file. Ensure that the server is configured to allow cross-origin requests by enabling the appropriate headers (e.g., Access-Control-Allow-Origin: *).
vivliosyn.com/python/main_1.py must open in the browser, but right now it's not opening and giving a 404 error. Please fix this issue."
|
0

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.

Comments

Your Answer

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.