HTML + Python

HtmPy is let’s you run Python code in a <script> tag on an HTML page when it’s shown on a WebView.

This module is also a bridge between Python and JavaScript so the code has access to the window object. However, the code can be very slow if too many JavaScript functions are called. This can be easily solved by doing the logic in Python and then calling a JavaScript function previously declared on a script tag to modify the DOM.

To take advantage of HtmPy, just create an HTML file and the editor will let you show it. Place your Python code in a script tag like that:

<script type="text/python">

To access the window object:

from htmpy import window

Then you can just call any function or get any attribute of the window object to modify the DOM. If you put JavaScript code on a tag before, you can get anything declared on the global scope through the window object. You can do the opposite, store a Python function or variable in the window object and it will be accessible from JavaScript. HtmPy will bridge Python functions so they can be called from JavaScript, so you could use addEventListener for example. However, the functions will run asynchronously and will not return any value.

You can use WebView to show HTML + Python pages on your custom UI.

class htmpy.WebView

A Web View that runs Python code in <script type="text/python"> tags.

did_receive_message(web_view, name, message)

A function called when a script message is received from a webpage. Takes the sender Web View, the name of the message and the content of the message as parameters.

The following example script shows how to send a message from a JavaScript page and how to receive it from the Web View.

import pyto_ui as ui

def did_receive_message(web_view, name, message):
    print(name, message)

web_view = ui.WebView()
web_view.did_receive_message = did_receive_message


<h1> Hello World </h1>



ui.show_view(web_view, ui.PRESENTATION_MODE_SHEET)
Return type:Callable[[WebView, str, object], None]
class htmpy.JSObject(id: str, web_view: pyto_ui.WebView)

A reference to a JavaScript object. The object is stored in a dictionary declared on the web page. An instance of this class holds the web view and the key of the referenced object in the dictionary.

To use an instance of this class, you can get / set an attribute or call it as a function (it it references a function). When getting or setting an attribute, certain types of values are automatically converted: string, number, boolean and arrays.

Do not manually create instances of JSObject, instead get them from window.