Python Forum
Creating a Browser Extension using Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating a Browser Extension using Python
#1
I am trying to create a Browser Extension that uses Python and have been unable to make my script work as an extension in any way.
I believe I need to make Javascript or HTML;
a) Call a Python script
b) Parse some information
c) Execute the script
d) Return the result.
OR -- IS THERE ANOTHER WAY TO ACHIEVE THIS??
AM I GOING ABOUT THIS THE WRONG WAY??
Below I have listed the different ways I have attempted;
1. Flask
I started by creating a Flask application. At the time I did not realise that Flask is primarily for creating browser applications (glorified websites) and not browser extensions. Hence why I abandoned this route.
2. PyScript
Following the Flask attempt, I found Pyscript. Again, this method did not work because PyScript did not support the libraries/modules that I use in my Python script.
3. Brython
I was unable to make Brython work because Brython can only import Pure Python libraries/modules. Some of the libraries I use are not Pure Python, hence why this method did not work.
4. Javascript/Different Language
I have been unable to convert my script to another, more browser friendly script due to the fact that I am using Python unique libraries/modules that do not appear in another language.
Unfortunately, as mentioned, I have failed in all my attempts and have wasted countless hours pursuing this.
I am hoping that someone has tried this in the past (or something like it) and knows how to accomplish this and/or can make some suggestions.
gracemartin likes this post
Reply
#2
Creating a Browser Extension with Python: Exploring Solutions

Developing a browser extension that incorporates Python functionality can be a challenging endeavor, but there are viable solutions to achieve this. Let's explore a recommended approach to seamlessly integrate Python into your browser extension.

Understanding the Challenge

Your goal is to create a browser extension that involves executing Python scripts, parsing information, and returning results. While Python isn't directly supported in browser environments, we can bridge the gap using a combination of technologies.

Recommended Solution: WebAssembly and JavaScript

Utilizing WebAssembly (Wasm) allows you to run Python code directly in the browser. Here's a step-by-step guide:

1. Choose a Python-to-Wasm Compiler:
- Look into tools like Pyodide or Pyodide-pack, which compile Python to WebAssembly. Pyodide provides a full Python runtime in the browser.

2. Setup a Basic Web Extension Structure:
- Create a basic HTML file and manifest.json for your extension. The HTML file will include necessary scripts and elements.

3. Integrate WebAssembly in HTML:
- Load the Python WebAssembly module in your HTML file using a <script> tag. This enables the execution of Python code within the browser.

4. Communication Between JavaScript and Python:
- Use JavaScript to handle user interactions, retrieve information, and communicate with the Python code. You can pass data between the two environments seamlessly.

5. Return Results to JavaScript:
- Ensure your Python code can return results to the JavaScript environment. This is crucial for further processing or displaying information to the user.

Benefits of Using WebAssembly

- Cross-Language Compatibility:
WebAssembly allows you to run code written in multiple languages, providing flexibility without compromising on performance.

- Efficient Execution:
Wasm executes at near-native speed, ensuring that your Python code performs well within the browser.

- Broad Browser Support:
Most modern browsers support WebAssembly, making it a reliable choice for cross-browser compatibility.


While Flask, PyScript, and Brython had limitations for your specific use case, leveraging WebAssembly with Python offers a more robust and versatile solution. By adopting this approach, you can seamlessly integrate Python scripts into your browser extension, enabling the execution of complex functionality directly within the user's browser environment.

Feel free to explore Pyodide or similar tools, and adapt your existing Python code to work efficiently with WebAssembly. This should set you on the right path to successfully creating your desired browser extension with Python functionality, Below are the links to the finest learning platforms
1. W3 School 2. link removed 3. JavaPoint

Remember to consult relevant documentation and resources for each tool to ensure a smooth integration process.

Good luck with your browser extension development! Thumbs Up
Larz60+ write Jan-25-2024, 11:23 AM:
removed spam link
Reply
#3
Thanks for your response. I am only 15 and learning to do this on my own so I really appreciate you taking time to assist me.

Your response left me with some questions.
I have previously looked at a WebAssembly, however I have been unable to find a tool (compiler) that supports non pure python libraries.
As I stated in my question, I have tried several different ways that follow a similar vein to the one you suggested (Brython/PyScript), however, since they stem from the same project (Pyodide) which does not support the libraries I require, I am unable to utilise these tools to complete my project.

As such, my question is this;
1. Do you know of a WebAssembly that does not require Pure Python Libraries?
OR
2. Do you have another suggested way to go about solving my problem?

Again, thanks for all of your assistance thus far. I can not claim to fully understand the intricacies of building a web extension but I am feeling my way. I hope that you will be able to assist me further.
Reply
#4
(Jan-19-2024, 03:19 AM)j49857 Wrote: I am trying to create a Browser Extension that uses Python and have been unable to make my script work as an extension in any way.
I believe I need to make Javascript or HTML;
a) Call a Python script
b) Parse some information
c) Execute the script
d) Return the result.
OR -- IS THERE ANOTHER WAY TO ACHIEVE THIS??
AM I GOING ABOUT THIS THE WRONG WAY??
Below I have listed the different ways I have attempted;
1. Flask
I started by creating a Flask application. At the time I did not realise that Flask is primarily for creating browser applications (glorified websites) and not browser extensions. Hence why I abandoned this route.
2. PyScript
Following the Flask attempt, I found Pyscript. Again, this method did not work because PyScript did not support the libraries/modules that I use in my Python script.
3. Brython
I was unable to make Brython work because Brython can only import Pure Python libraries/modules. Some of the libraries I use are not Pure Python, hence why this method did not work.
4. Javascript/Different Language
I have been unable to convert my script to another, more browser friendly script due to the fact that I am using Python unique libraries/modules that do not appear in another language.
Unfortunately, as mentioned, I have failed in all my attempts and have wasted countless hours pursuing this.
I am hoping that someone has tried this in the past (or something like it) and knows how to accomplish this and/or can make some suggestions.

Thanks for your response. I am only 15 and learning to do this on my own so I really appreciate you taking time to assist me.

Your response left me with some questions.
I have previously looked at a WebAssembly, however I have been unable to find a tool (compiler) that supports non pure python libraries.
As I stated in my question, I have tried several different ways that follow a similar vein to the one you suggested (Brython/PyScript), however, since they stem from the same project (Pyodide) which does not support the libraries I require, I am unable to utilise these tools to complete my project.

As such, my question is this;
1. Do you know of a WebAssembly that does not require Pure Python Libraries?
OR
2. Do you have another suggested way to go about solving my problem?

Again, thanks for all of your assistance thus far. I can not claim to fully understand the intricacies of building a web extension but I am feeling my way. I hope that you will be able to assist me further.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Expose chrome extension buttons to Python robertjaxe 2 2,359 May-12-2020, 07:52 PM
Last Post: robertjaxe
  webdriver.remote to connect back existing browser without open new browser? gahhon 6 6,788 Feb-26-2019, 03:53 PM
Last Post: gahhon
  Enable error logging to browser for Python's Flask under Apache + mod_wsgi nikos 1 3,074 Sep-18-2018, 09:15 PM
Last Post: nikos
  I Want To Download Many Files Of Same File Extension With Either Wget Or Python, eddywinch82 15 14,562 May-20-2018, 06:05 PM
Last Post: eddywinch82
  Is it possible to import / run compiled Python (.pyd) file in web browser? dvid 4 9,437 Jan-08-2018, 09:26 AM
Last Post: SarbjitGrewal

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020