Files
pywebview/webview/window.py
Roman 0698b059db pywebview 3.0 (#340)
* Update change_url.md

* Update api.md

* Update installation.md

* Add Google Analytics

* Fix installation

* 2.4 info

* [Docs] add information about window.pywebview availability

* Update debuggig info about QTWebEngine

* [Docs] Add information about app architecture

* Update cef.md

* [CEF] Fix JS logic not persisting after navigating to another page

* #169 Frameless window (#313)

* Merge PR #177: [Cocoa] Implement frameless windows

* [All] Add skeleton for `frameless` parameter
* [Cocoa] Add `frameless` kwarg to remove the window frame.
  - Implement a DragBar class to enable dragging the webview
  - Make the drag bar stick to the top and autoresize to the width
    of the view.

* [WinForms] Implement frameless window + easy drag

* Merge PR #177: [Cocoa] Implement frameless windows

* [All] Add skeleton for `frameless` parameter
* [Cocoa] Add `frameless` kwarg to remove the window frame.
  - Implement a DragBar class to enable dragging the webview
  - Make the drag bar stick to the top and autoresize to the width
    of the view.

* [WinForms] Implement frameless window + easy drag

* [GTK] partial implementation of frameless window

* [QT] Partial implementation of frameless window

* [QT] implement easy drag

* [Cocoa] Hide standard buttons and make the entire window draggable

* [GTK] implement easy move

* Add frameless tests

* Remove unused parameter

* [Cocoa] Implement free drag

* Revert WebBrowserInterops

* Code cleanup

* Update interop DLLs

* Fix interop dll

* Update docs; Fix tests

* Add example

* Update examples

* [CEF]: Implement support for pyinstaller one-file mode

* [CEF] Fix typo

* [All] Change api_call timeout to 15 seconds

* Update setup.py

* [QT] Simplify QT installation

* New API
Cocoa implementation
Revamped tests and examples

* Fix web engine force setting

* Rename destroy_window to destroy

* Update documentation

* Add documentation for 2.4

* Refactor project structure

* [GTK] New API support

* Rename confirm_quit to confirm_close
Split lifecycle events to shown and loaded

* [GTK] Implement new API
Fix tests

* [QT] New API implementation

* Remove multiprocessing

* [Windows/CEF] New API implementation

* Implement CSRF token
[All] Implement get_element

* Update Flask app example

* Rename get_element to get_elements. Bug fixes.

* [GTK] Implement propert JS-Python bridge for WebKit2 >2.2

* Update documentation

* [Windows/CEF] New API implementation

* Add token test

* [Winforms] Fix js api initialization

* [Cocoa] Fix events

* Debug: print webkit version

* [GTK] Check for old webkit

* [GTK] Fix event issues

* Catch notimlementederror in tests

* Update travis conf with qt from pip

* Update .travis.yml

* Update .travis.yml

* Update .travis.yml

* Update .travis.yml

* Update .travis.yml

* [Windows] Preliminary implementation of EdgeHTML based on PR from @HeavenVolkoff

* [docs] Add information about EdgeHTML

* EdgeHTML WIP

* Remove windows.web import

* EdgeHTML improvements. Fix MSHTML evaluate_js

* [Docs] Add information about renderer limitations

* [EdgeHTML] Fix bugs and tests

* Remove window from windows list after it is destroyed

* Fix window object deletion after window is closed via GUI

* [Docs] Add information about renderer codes

* Refactor and fix JS API
Remove multiprocessing example
[MSHTML] Add console.log debug support

* [Travis] Change conf

* [WinForms] Fix fullscreen

* Fix dll installation

* [Windows] Fix DLL bundling

* Update changelog

* [CEF]: Fix JS API

* Update changelog

* Built-in HTTP server implementation

* Add http server test

* Add http server testcd

* Add http server testcd

* [EdgeHTML] allow localhost urls

* Add video example

* Bug fixing

* [Cocoa] Remove media prefs

* Update .travis.yml

* Update .travis.yml

* Update .travis.yml

* [Travis] Disable QT 5.12 builds

* Update html_load.md
2019-07-07 13:33:37 +03:00

190 lines
6.4 KiB
Python

import os
from functools import wraps
from webview.event import Event
from webview.http_server import start_server
from webview.util import base_uri, parse_file_type, escape_string, transform_url, make_unicode, WebViewException
from .js import css
def _api_call(function, event_type):
"""
Decorator to call a pywebview API, checking for _webview_ready and raisings
appropriate Exceptions on failure.
"""
@wraps(function)
def wrapper(*args, **kwargs):
event = args[0].loaded if event_type == 'loaded' else args[0].shown
try:
if not event.wait(15):
raise WebViewException('Main window failed to start')
if args[0].gui is None:
raise WebViewException('GUI is not initialized')
return function(*args, **kwargs)
except NameError as e:
raise WebViewException('Create a web view window first, before invoking this function')
return wrapper
def _shown_call(function):
return _api_call(function, 'shown')
def _loaded_call(function):
return _api_call(function, 'loaded')
class Window:
def __init__(self, uid, title, url, html, width, height, resizable, fullscreen,
min_size, confirm_close, background_color, js_api, text_select, frameless):
self.uid = uid
self.title = make_unicode(title)
self.url = None if html else transform_url(url)
self.html = html
self.width = width
self.height = height
self.resizable = resizable
self.fullscreen = fullscreen
self.min_size = min_size
self.confirm_close = confirm_close
self.background_color = background_color
self.js_api = js_api
self.text_select = text_select
self.frameless = frameless
self.loaded = Event()
self.shown = Event()
self.gui = None
def _initialize(self, gui, multiprocessing, http_server):
self.gui = gui
self.loaded._initialize(multiprocessing)
self.shown._initialize(multiprocessing)
if http_server and self.url.startswith('file://'):
self.url = start_server(self.url)
@_loaded_call
def get_elements(self, selector):
# check for GTK's WebKit2 version
if hasattr(self.gui, 'old_webkit') and self.gui.old_webkit:
raise NotImplementedError('get_elements requires WebKit2 2.2 or greater')
code = """
var elements = document.querySelectorAll('%s');
var serializedElements = [];
for (var i = 0; i < elements.length; i++) {
var node = pywebview.domJSON.toJSON(elements[i], {
metadata: false,
serialProperties: true
});
serializedElements.push(node);
}
serializedElements;
""" % selector
return self.evaluate_js(code)
@_shown_call
def load_url(self, url):
"""
Load a new URL into a previously created WebView window. This function must be invoked after WebView windows is
created with create_window(). Otherwise an exception is thrown.
:param url: url to load
:param uid: uid of the target instance
"""
self.gui.load_url(url, self.uid)
@_shown_call
def load_html(self, content, base_uri=base_uri()):
"""
Load a new content into a previously created WebView window. This function must be invoked after WebView windows is
created with create_window(). Otherwise an exception is thrown.
:param content: Content to load.
:param base_uri: Base URI for resolving links. Default is the directory of the application entry point.
:param uid: uid of the target instance
"""
content = make_unicode(content)
self.gui.load_html(content, base_uri, self.uid)
@_loaded_call
def load_css(self, stylesheet):
code = css.src % stylesheet.replace('\n', '').replace('\r', '').replace('"', "'")
self.gui.evaluate_js(code, self.uid)
@_shown_call
def set_title(self, title):
"""
Set a new title of the window
"""
self.gui.set_title(title, self.uid)
@_loaded_call
def get_current_url(self):
"""
Get the URL currently loaded in the target webview
"""
return self.gui.get_current_url(self.uid)
@_shown_call
def destroy(self):
"""
Destroy a web view window
"""
self.gui.destroy_window(self.uid)
@_shown_call
def toggle_fullscreen(self):
"""
Toggle fullscreen mode
"""
self.gui.toggle_fullscreen(self.uid)
@_shown_call
def set_window_size(self, width, height):
"""
Set Window Size
:param width: desired width of target window
:param height: desired height of target window
"""
self.gui.set_window_size(width, height, self.uid)
@_loaded_call
def evaluate_js(self, script):
"""
Evaluate given JavaScript code and return the result
:param script: The JavaScript code to be evaluated
:return: Return value of the evaluated code
"""
escaped_script = 'JSON.stringify(eval("{0}"))'.format(escape_string(script))
return self.gui.evaluate_js(escaped_script, self.uid)
@_shown_call
def create_file_dialog(self, dialog_type=10, directory='', allow_multiple=False, save_filename='', file_types=()):
"""
Create a file dialog
:param dialog_type: Dialog type: open file (OPEN_DIALOG), save file (SAVE_DIALOG), open folder (OPEN_FOLDER). Default
is open file.
:param directory: Initial directory
:param allow_multiple: Allow multiple selection. Default is false.
:param save_filename: Default filename for save file dialog.
:param file_types: Allowed file types in open file dialog. Should be a tuple of strings in the format:
filetypes = ('Description (*.extension[;*.extension[;...]])', ...)
:return: A tuple of selected files, None if cancelled.
"""
if type(file_types) != tuple and type(file_types) != list:
raise TypeError('file_types must be a tuple of strings')
for f in file_types:
parse_file_type(f)
if not os.path.exists(directory):
directory = ''
return self.gui.create_file_dialog(dialog_type, directory, allow_multiple, save_filename, file_types, self.uid)