Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
WKWebView - modern webview for Pythonista
-
Some updates:
User scripts a.k.a. script injection
WKWebView supports defining JS scripts that will be automatically loaded with every page.
Use the
add_script(js_script, add_to_end=True)
method for this.Scripts are added to all frames. Removing scripts is currently not implemented.
Following two convenience methods are also available:
add_style(css)
to add a style tag containing the given CSS style definition.add_meta(name, content)
to add a meta tag with the given name and content.
Making a web page behave more like an app
These methods set various style and meta tags to disable typical web interaction modes:
disable_zoom
disable_user_selection
disable_font_resizing
disable_scrolling
(alias for settingscroll_enabled
to False)
There is also a convenience method,
disable_all
, which calls all of the above. -
Javascript debugging
Javascript errors and console messages are sent to Python side and printed to Pythonista console. Supported JS console methods are
log
,info
,warn
anderror
.For further JS debugging and experimentation, there is a simple convenience command-line utility that can be used to evaluate load URLs and evaluate javascript. If you
present
your app as a 'panel', you can easily switch between the tabs for your web page and this console.Or you can just create a WKWebView manually for quick experimentation, like in the usage example below.
>>> from wkwebview import * >>> v = WKWebView(name='Demo') >>> WKWebView.console() Welcome to WKWebView console. Evaluate javascript in any active WKWebView. Special commands: list, switch #, load <url>, quit js> list 0 - Demo - js> load http://omz-software.com/pythonista/ js> list 0 - Demo - Pythonista for iOS js> document.title Pythonista for iOS js> quit >>> v2 = WKWebView(name='Other view') >>> WKWebView.console() Welcome to WKWebView console. Evaluate javascript in any active WKWebView. Special commands: list, switch #, load <url>, quit js> list 0 - Demo - Pythonista for iOS 1 - Other view - js> switch 1 js> load https://www.python.org js> document.title Welcome to Python.org js> window.doesNotExist.wrongFunction() ERROR: TypeError: undefined is not an object (evaluating 'window.doesNotExist.wrongFunction') (https://www.python.org/, line: 1, column: 20) None js> quit
-
@mikael I like that .... ;-)
-
@mithrendal, well, the credit for the whole console idea belongs to you. I looked at your code and experimented with keyboard auxiliary view for entering the javascript code and so on and so forth, but in the end I decided on this option because it adds very few lines of code to the package.
The one thing I would still like to be able to do, though, is add the lines entered in the ”js console” to the Pythonista console history, so that the history buttons would work naturally. @omz, is there anything exposed that could be used for this?
-
not sure if this still works. but the basic approach does:
cvc=UIApplication.sharedApplication().\ keyWindow().rootViewController().\ accessoryViewController().\ consoleViewController cvc.history()
gives you access to console history, which is just an NSArray of NSStrings(newline terminated)
.cvc.history=[ns('hello\n')] + list(cvc.history())
or maybe with a little less copying, though not sure if you are always guaranteed a mutable array like this:
cvc.history().insertObject_atIndex_(ns('hi\n'),0)
(seems to work, but safer might be to call mutableCopy, then do the insert, and assign back)
-
@JonB, sweet! Works perfectly & committed.
-
Moved the code to a dedicated repository and split the documentation into a README.
Couple of small functionality updates:
Cache and timeouts
For remote (non-file)
load_url
requests, there are two additional options:- Set
no_cache
toTrue
to skip the local cache, default isFalse
- Set
timeout
to a specific timeout value, default is 10 (seconds)
Setting a custom user agent
WKWebView has a
user_agent
property that can be used to retrieve or set a value reported to the server when requesting pages. - Set
-
@mikael thanks for this great module. Just wanted to mention that the only way I was able to get pydrive to work with google/oauth authentication flow was by using WKWebView and setting the user agent.
-
Couple of additions, thanks @Brun0oO.
Cache and timeouts
For remote (non-file)
load_url
requests, there are two additional options:- Set
no_cache
toTrue
to skip the local cache, default isFalse
- Set
timeout
to a specific timeout value, default is 10 (seconds)
You can also explicitly clear all data types from the default data store with the
clear_cache
instance method. The method takes an optional parameter, a plain function that will be called when the async cache clearing operation is finished:def cleared(): print('Cache cleared') WKWebView().clear_cache(cleared)
Media playback
Following media playback options are available as WKWebView constructor parameters:
inline_media
- whether HTML5 videos play inline or use the native full-screen controller. The default value for iPhone is False and the default value for iPad is True.airplay_media
- whether AirPlay is allowed. The default value is True.pip_media
- whether HTML5 videos can play picture-in-picture. The default value is True.
- Set
-
This post is deleted!