'How to use python program for a web extension?

I am making a simple web extension. I want to attach a python program to its files so that the python program can use the data entered by the user in the extension popup for a particular operation and the output will be displayed on the extension popup.

Please guide me in this process. It's my first time making a web extension.



Solution 1:[1]

I don't think you can execute python script inside javascript file. these are two option you can use:-

  1. create api with python script and call api inside you javascript file.
  2. explore pyscript , Pyscript is used for using python script inside html code.(I have not tried it yet)

Solution 2:[2]

You can use PyScript to write your entire extension with Python.

Take a look at this simple example of running python and DOM manipulating:

<html>
    <head>
        <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
        <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
         <style>
            h1 {
                font-size: 20px; 
                color: green; 
                text-transform: uppercase;
                text-align: center; 
                margin: 0 0 35px 0; 
                text-shadow: 0px 1px 0px #f2f2f2;
            }
        </style>
    </head>
    <body>
    <h1>PyScript Events</h1>
    <div>
        <p> I am just a random element </p>
        <p id="change_me"> I think PyScript is awesome </p>
        <p> I like hamburgers  </p>
    </div>
    
    <py-script>
import pyodide

def on_click(e):
    e.target.innerHTML = "PyScript is really awesome!"
    
change_me_element = document.getElementById('change_me')
change_me_element.addEventListener('click', pyodide.create_proxy(on_click))
    </py-script>
    
    </body>

Here you have a great tutorial about getting started with PyScript.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hackCharms
Solution 2 PleSo