Home > Net >  How to run a python or .exe file in html page?
How to run a python or .exe file in html page?

Time:11-05

I tried running my game on a html website, but it didn't work. can i use html or css or javascript to open and application or .exe

i tried using iframe but it just shows me the parent directory [This is the image i receive] (https://i.stack.imgur.com/kUChj.png) (https://i.stack.imgur.com/kUChj.png) i also tried the anchor function in html it didnt work either

CodePudding user response:

Recently thanks yo PyScript is possible to run a python script within a HTML page in the new <py-script> tag, by example

 <py-script>
   from datetime import datetime
   now = datetime.now()
   now.strftime("%m/%d/%Y, %H:%M:%S")
 </py-script>

You can see the Python source code within the <py-script> and </py-script> tags that go within the <body> of a <html> tag. Here it is a fully working example, click on "full page" to see the output in the html page, that should be like

Hello world!
This is the current date and time, as computed by Python:
11/04/2022, 21:52:53

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1" />

    <title>PyScript Hello World</title>

    <link rel="icon" type="image/png" href="favicon.png" />
    <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />

    <script defer src="https://pyscript.net/latest/pyscript.js"></script>
  </head>

  <body>
    Hello world! <br>
    This is the current date and time, as computed by Python:
    <py-script>
from datetime import datetime
now = datetime.now()
now.strftime("%m/%d/%Y, %H:%M:%S")
    </py-script>
  </body>
</html>

The magic happens thanks to Pyscript Web Assembly (WASM) Python interpreter called Pyodide, that is automatically loaded when injecting in the page a <script> tag that will load PyScript, parse the <py-script> tag and run the Python code right in the page!

<script defer src="https://pyscript.net/latest/pyscript.js">

It is worth to note that a lot of Python pip packages can be loaded in the page as well thanks to micropip that will work in the same way of Pypi pip, but in the html page, and thanks to PyScript engine it only needs to write a new <py-config> tag, having the packages to install:

<py-config>
    packages = [
      "matplotlib"
    ]
</py-config>

A good example is running the popular scientific visualization library matplotlib straight in the browser with that approach. Working example here.

CodePudding user response:

No, you cannot use HTML, CSS, or JavaScript to open an application or .exe file

If it is python you would have to serve html files / http responses after making a http server via flask, django or the very basic ones. It is not such a simple thing as just iframing a python file or a packaged exe

CodePudding user response:

You can use CPython to compile your Python script to a WebAssembly module (WASM). Then you can load the WASM module in your HTML page and run it by calling it from JavaScript.

  • Related