irthomasthomas / undecidability

1 stars 0 forks source link

pywebview: a Python library that lets you build a GUI for your Python program using HTML, CSS, and Javascript. #756

Open irthomasthomas opened 2 months ago

irthomasthomas commented 2 months ago

pywebview

DESCRIPTION: pywebview 5. The new version introduces three major features: Android support, DOM manipulation, and application settings. For a full changelog, see here.

If you are not familiar with pywebview, it is a Python library that lets you build a GUI for your Python program using HTML, CSS, and Javascript. Available for Windows, macOS, Linux, and Android. pywebview can be installed with

pip install pywebview

Android

You can now run your pywebview on Android devices. Mobile experience brings its own limitations though. There is no window manipulation, multi-window, or file dialog support. Otherwise, it works the same as on other platforms. Head over to Freezing for details on how to package your app for Android.

DOM

With DOM support, you can perform jQuery-like DOM manipulation, traversal, and event handling straight from Python. You can access and modify element's attributes, style, and classes as well. A new Element object represents a DOM node in Python. It is returned by window.dom.get_element, window.dom.get_elements, and window.dom.create_element. Body, document, and window are conveniently exposed as window.dom.body, window.dom.document, and window.dom.body respectively. The new Javascript serializer allows you to serialize more Javascript object types and handles circular dependencies.

Here is a toy example of the new API.

window.dom.document.events.scroll += lambda e: print(window.dom.window.node['scrollY'])

button = window.dom.create_element('<button disabled class=\"hidden\">Button</button>', window.dom.body)
button.style['width'] = '200px'
button.attributes = { 'disabled': False }
button.events.click += click_handler
button.classes.toggle('hidden')

See events, manipulation, events for complete examples.

A much-requested feature is full file path support for drag and drop operations. pywebview enhances DropEvent by introducing event['dataTransfer']['files'][0]['pywebviewFullPath'] that has the full absolute path of a dropped file(s). The full path is available only on Python's side.

Application settings

pywebview is rather opinionated on how the default experience should be. Over the years, I have received numerous feature requests asking to change the default behavior, which is now possible with application settings. The new version introduces webview.settings dictionary with the following options.

webview.settings = {
    'ALLOW_DOWNLOADS': False, # Allow file downloads
    'ALLOW_FILE_URLS': True, # Allow access to file:// urls
    'OPEN_EXTERNAL_LINKS_IN_BROWSER': True, # Open target=_blank links in an external browser
    'OPEN_DEVTOOLS_IN_DEBUG': True, # Automatically open devtools when `start(debug=True)`.
}

Application settings must be set before invoking webview.start() to have an effect.

URL: https://pywebview.flowrl.com/blog/pywebview5.html

Suggested labels

{'label-name': 'python-libraries', 'label-description': 'Topics related to Python libraries and their functionalities.', 'gh-repo': 'web-development', 'confidence': 53.87}

irthomasthomas commented 2 months ago

Related content

674

Similarity score: 0.85

508

Similarity score: 0.85

675

Similarity score: 0.85

743

Similarity score: 0.83

399

Similarity score: 0.82

683

Similarity score: 0.82