Getting Started
To use BrowserPie in your web application, you need to include the BrowserPie library. Add the following script tag to your HTML file:
<script src="https://browserpie.pages.dev/lib.js"></script>
This library enables your web pages to run Python code directly in the browser.
You can also use this api on scratch. (extension)
Function Overview
BrowserPie provides a set of functions to interact with Python in your browser. Below are the available functions along with their descriptions and examples of how to use them.
1. BrowserPie.setPort(port)
Purpose: Changes the port that your site listens on. This is necessary if the user has changed the port on which the BrowserPie server is running. The default port is 5938.
Usage: Use this function only if you need to adjust the port for your site to match the port set by the BrowserPie server.
BrowserPie.setPort(5938); // Sets the port to 5938 (default)
2. BrowserPie.openHistory()
Purpose: Opens the BrowserPie script history page in a new tab.
BrowserPie.openHistory(); // Opens the history page
3. BrowserPie.isInstalled()
Purpose: Checks if BrowserPie is installed on the selected port. Returns true if installed, false otherwise.
Usage: This function is asynchronous, so you must use it within an async context. Call it to confirm that BrowserPie is set up correctly before attempting to run Python code.
const installed = await BrowserPie.isInstalled();
if (installed) {
console.log("BrowserPie is installed.");
} else {
console.log("BrowserPie is not installed.");
}
4. BrowserPie.isAllowed()
Purpose: Checks if the current website is allowed to execute Python code. If not, it shows a prompt to the user to grant permission.
Usage: This function is asynchronous and should be called before running any Python code to ensure the site has the necessary permissions.
const allowed = await BrowserPie.isAllowed();
if (allowed) {
console.log("This site is allowed to run Python code.");
} else {
console.log("This site is not allowed to run Python code.");
}
5. BrowserPie.run(code)
Purpose: Executes the given Python code string.
Usage: This function is also asynchronous. Use it to run any Python script you want. Make sure the script is a valid Python code string.
await BrowserPie.run('import ctypes; ctypes.windll.user32.MessageBoxW(0, "Hello, World!", "Alert", 1)'); // SHowing an alert box