Debian Codegraph @

docs/.vuepress/public/3.7/blog/pywebview3.html

raw
1 <!DOCTYPE html>
2 <html lang="en-US">
3 <head>
4 <meta charset="utf-8">
5 <meta name="viewport" content="width=device-width,initial-scale=1">
6 <title>pywebview</title>
7 <meta name="generator" content="VuePress 1.9.7">
8
9 <meta name="description" content="Build GUI for your Python program with JavaScript, HTML, and CSS">
10
11 <link rel="preload" href="/3.7/assets/css/0.styles.28a501df.css" as="style"><link rel="preload" href="/3.7/assets/js/app.10a559eb.js" as="script"><link rel="preload" href="/3.7/assets/js/2.0ff7ef1d.js" as="script"><link rel="preload" href="/3.7/assets/js/6.64e1f3ed.js" as="script"><link rel="prefetch" href="/3.7/assets/js/10.f125ec08.js"><link rel="prefetch" href="/3.7/assets/js/11.81fead78.js"><link rel="prefetch" href="/3.7/assets/js/12.0a86c191.js"><link rel="prefetch" href="/3.7/assets/js/13.c09145fc.js"><link rel="prefetch" href="/3.7/assets/js/14.2a7dc35b.js"><link rel="prefetch" href="/3.7/assets/js/15.90c9f36a.js"><link rel="prefetch" href="/3.7/assets/js/16.69b1f4b4.js"><link rel="prefetch" href="/3.7/assets/js/17.956ed884.js"><link rel="prefetch" href="/3.7/assets/js/18.bd6678a0.js"><link rel="prefetch" href="/3.7/assets/js/19.b31e62b7.js"><link rel="prefetch" href="/3.7/assets/js/20.cebc4a71.js"><link rel="prefetch" href="/3.7/assets/js/21.d2041966.js"><link rel="prefetch" href="/3.7/assets/js/22.2a3916f2.js"><link rel="prefetch" href="/3.7/assets/js/23.712fbf84.js"><link rel="prefetch" href="/3.7/assets/js/24.f768e66b.js"><link rel="prefetch" href="/3.7/assets/js/25.69b3e415.js"><link rel="prefetch" href="/3.7/assets/js/26.6619f84a.js"><link rel="prefetch" href="/3.7/assets/js/27.e2a4e1bf.js"><link rel="prefetch" href="/3.7/assets/js/28.abcc819c.js"><link rel="prefetch" href="/3.7/assets/js/29.c379d080.js"><link rel="prefetch" href="/3.7/assets/js/3.749f6582.js"><link rel="prefetch" href="/3.7/assets/js/30.771e8640.js"><link rel="prefetch" href="/3.7/assets/js/31.547a8393.js"><link rel="prefetch" href="/3.7/assets/js/32.778a82c7.js"><link rel="prefetch" href="/3.7/assets/js/33.6ff70f11.js"><link rel="prefetch" href="/3.7/assets/js/34.ae1b5fb4.js"><link rel="prefetch" href="/3.7/assets/js/35.c5555b94.js"><link rel="prefetch" href="/3.7/assets/js/36.d53c471f.js"><link rel="prefetch" href="/3.7/assets/js/37.bbba2d58.js"><link rel="prefetch" href="/3.7/assets/js/38.5eb30036.js"><link rel="prefetch" href="/3.7/assets/js/39.8cac65fe.js"><link rel="prefetch" href="/3.7/assets/js/4.f2c64e71.js"><link rel="prefetch" href="/3.7/assets/js/40.53182a9c.js"><link rel="prefetch" href="/3.7/assets/js/41.19399765.js"><link rel="prefetch" href="/3.7/assets/js/42.41128068.js"><link rel="prefetch" href="/3.7/assets/js/43.660f3959.js"><link rel="prefetch" href="/3.7/assets/js/44.09d5c6f8.js"><link rel="prefetch" href="/3.7/assets/js/45.4a818eab.js"><link rel="prefetch" href="/3.7/assets/js/46.d1621897.js"><link rel="prefetch" href="/3.7/assets/js/47.7f8f8dec.js"><link rel="prefetch" href="/3.7/assets/js/48.f27d4c39.js"><link rel="prefetch" href="/3.7/assets/js/49.4189d401.js"><link rel="prefetch" href="/3.7/assets/js/5.2a771d7d.js"><link rel="prefetch" href="/3.7/assets/js/50.beff8380.js"><link rel="prefetch" href="/3.7/assets/js/51.8a918a6c.js"><link rel="prefetch" href="/3.7/assets/js/52.92d0105f.js"><link rel="prefetch" href="/3.7/assets/js/53.28701cde.js"><link rel="prefetch" href="/3.7/assets/js/54.2b15d6fb.js"><link rel="prefetch" href="/3.7/assets/js/55.d231c742.js"><link rel="prefetch" href="/3.7/assets/js/56.7e02bbff.js"><link rel="prefetch" href="/3.7/assets/js/57.61e9e46c.js"><link rel="prefetch" href="/3.7/assets/js/58.cc70e253.js"><link rel="prefetch" href="/3.7/assets/js/59.d3f412e7.js"><link rel="prefetch" href="/3.7/assets/js/60.f4fd7728.js"><link rel="prefetch" href="/3.7/assets/js/61.cd0f1d68.js"><link rel="prefetch" href="/3.7/assets/js/62.bf000bcc.js"><link rel="prefetch" href="/3.7/assets/js/63.807edde1.js"><link rel="prefetch" href="/3.7/assets/js/64.2b6fe843.js"><link rel="prefetch" href="/3.7/assets/js/7.779cbcff.js"><link rel="prefetch" href="/3.7/assets/js/8.66fb3acb.js"><link rel="prefetch" href="/3.7/assets/js/9.84350af9.js">
12 <link rel="stylesheet" href="/3.7/assets/css/0.styles.28a501df.css">
13 </head>
14 <body>
15 <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/3.7/" class="home-link router-link-active"><img src="/3.7/logo-no-text.png" alt="pywebview" class="logo"> <span class="site-name can-hide">pywebview</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/3.7/guide/" class="nav-link">
16 Guide
17 </a></div><div class="nav-item"><a href="/3.7/examples/" class="nav-link">
18 Examples
19 </a></div><div class="nav-item"><a href="/3.7/contributing/" class="nav-link">
20 Contributing
21 </a></div><div class="nav-item"><a href="/3.7/blog/" class="nav-link router-link-active">
22 Blog
23 </a></div><div class="nav-item"><a href="/3.7/CHANGELOG.html" class="nav-link">
24 Changelog
25 </a></div> <a href="https://github.com/r0x0r/pywebview" target="_blank" rel="noopener noreferrer" class="repo-link">
26 GitHub
27 <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/3.7/guide/" class="nav-link">
28 Guide
29 </a></div><div class="nav-item"><a href="/3.7/examples/" class="nav-link">
30 Examples
31 </a></div><div class="nav-item"><a href="/3.7/contributing/" class="nav-link">
32 Contributing
33 </a></div><div class="nav-item"><a href="/3.7/blog/" class="nav-link router-link-active">
34 Blog
35 </a></div><div class="nav-item"><a href="/3.7/CHANGELOG.html" class="nav-link">
36 Changelog
37 </a></div> <a href="https://github.com/r0x0r/pywebview" target="_blank" rel="noopener noreferrer" class="repo-link">
38 GitHub
39 <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <!----> </aside> <main class="page"> <div class="theme-default-content content__default"><img src="/3.7/assets/img/pywebview3.5e63e895.png" alt="pywebview 3.0" style="max-width:300px;margin:50px auto;display:block;"> <h1 id="introducing-pywebview-3-0"><a href="#introducing-pywebview-3-0" class="header-anchor">#</a> Introducing pywebview 3.0</h1> <p>I am happy to announce the release of <em>pywebview 3.0</em>. <em>pywebview</em> lets you to build GUI for your Python program using HTML, CSS and Javascript, while doing its best hiding the fact that the GUI is built using a browser. Think of <em>pywebview</em> as lightweight Electron for Python. Unlike Electron, <em>pywebview</em> does not bundle a web renderer, but instead relies on a rendered provided by operating system. <em>Sidenote: bundling a renderer is still an option though, as in case of CEF</em>.</p> <p>If you are new here, head over to <a href="/3.7/guide/usage.html">usage guide</a>, <a href="/3.7/guide/api.html">API reference</a>, <a href="/examples">examples</a> and our very own <a href="https://github.com/r0x0r/pywebview/tree/master/examples/todos" target="_blank" rel="noopener noreferrer">TODOs app<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>.</p> <p>Oh and <em>pywebview</em> can be installed with</p> <div class="language-bash extra-class"><pre class="language-bash"><code>pip <span class="token function">install</span> pywebview
40 </code></pre></div><h2 id="what-s-new"><a href="#what-s-new" class="header-anchor">#</a> What's new?</h2> <p>Version 3.0 is the first version that is not compatible with previous versions. Multi-window support introduced in 2.x resulted in some questionable architectural decisions, which now have been resolved and hopefully make more sense. Notable changes include:</p> <h3 id="webview-start"><a href="#webview-start" class="header-anchor">#</a> webview.start()</h3> <p>The biggest change is introduction of window objects and <code>webview.start()</code> function that starts a GUI loop. Previously GUI loop was started by the first call of <code>webview.create_window()</code>. Hence <code>create_window</code> had in fact two functions: creating a window and starting a GUI loop. To make things more confusing the first call to <code>create_window</code> was blocking, while subsequent calls from subthreads were not. To make things more straightforward, <code>create_window</code> now creates a window and returns a window object, no matter how many times you call it. The function is always non-blocking too. Bear in mind that until GUI loop is started, no windows are displayed. Using new API, hello world in <em>pywebview</em> looks like this:</p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">import</span> webview
41
42 window <span class="token operator">=</span> webview<span class="token punctuation">.</span>create_window<span class="token punctuation">(</span><span class="token string">'Hello world'</span><span class="token punctuation">,</span> <span class="token string">'https://pywebview.flowrl.com/hello'</span><span class="token punctuation">)</span>
43 webview<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span>
44 </code></pre></div><p><code>webview.start</code> also provides a convenient way to execute thread specific code after GUI loop is started, so no more threading boilerplate.</p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">import</span> webview
45
46 <span class="token keyword">def</span> <span class="token function">change_title</span><span class="token punctuation">(</span>window<span class="token punctuation">)</span><span class="token punctuation">:</span>
47 window<span class="token punctuation">.</span>change_title<span class="token punctuation">(</span><span class="token string">'pywebview whoa'</span><span class="token punctuation">)</span>
48
49 window <span class="token operator">=</span> webview<span class="token punctuation">.</span>create_window<span class="token punctuation">(</span><span class="token string">'pywebview wow'</span><span class="token punctuation">,</span> <span class="token string">'https://pywebview.flowrl.com/hello'</span><span class="token punctuation">)</span>
50 webview<span class="token punctuation">.</span>start<span class="token punctuation">(</span>change_title<span class="token punctuation">,</span> window<span class="token punctuation">)</span>
51 </code></pre></div><h3 id="window-object"><a href="#window-object" class="header-anchor">#</a> Window object</h3> <p>All the functions related to window management and web content have been moved to a window object as returned by <code>webview.create_window</code>. For example <code>webview.load_html</code> became <code>window.load_html</code> as in:</p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">import</span> webview
52
53 <span class="token keyword">def</span> <span class="token function">load_html</span><span class="token punctuation">(</span>window<span class="token punctuation">)</span><span class="token punctuation">:</span>
54 window<span class="token punctuation">.</span>load_html<span class="token punctuation">(</span><span class="token string">'&lt;html&gt;&lt;body&gt;&lt;h1&gt;pywebview wow!&lt;/h1&gt;&lt;body&gt;&lt;/html&gt;'</span><span class="token punctuation">)</span>
55
56 window <span class="token operator">=</span> webview<span class="token punctuation">.</span>create_window<span class="token punctuation">(</span><span class="token string">'pywebview wow'</span><span class="token punctuation">)</span>
57 webview<span class="token punctuation">.</span>start<span class="token punctuation">(</span>load_html<span class="token punctuation">,</span> window<span class="token punctuation">)</span>
58 </code></pre></div><h3 id="built-in-http-server"><a href="#built-in-http-server" class="header-anchor">#</a> Built-in HTTP server</h3> <p><em>pywebview</em> now provides its own HTTP server for serving static local files. For obfuscation purposes server is started on a random port.</p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">import</span> webview
59
60 window <span class="token operator">=</span> webview<span class="token punctuation">.</span>create_window<span class="token punctuation">(</span><span class="token string">'pywebview wow'</span><span class="token punctuation">,</span> <span class="token string">'assets/index.html'</span><span class="token punctuation">)</span>
61 webview<span class="token punctuation">.</span>start<span class="token punctuation">(</span>http_server<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
62 </code></pre></div><h3 id="events"><a href="#events" class="header-anchor">#</a> Events</h3> <p>3.0 introduces a new event system that lets to subscribe/unsubscribe to events. Currently <code>shown</code> and <code>loaded</code> events are implemented. Event objects are provided by a window object. See <a href="/3.7/examples/events.html">events example</a> for usage details.</p> <h3 id="edge-support"><a href="#edge-support" class="header-anchor">#</a> Edge support</h3> <p>Windows now provides support for EdgeHTML. EdgeHTML is automatically chosen if your system requirements are met (.NET 4.6.2 and Windows 10 1803). Unfortunately accessing local files is not currently possible with EdgeHTML, so you must use a HTTP server. If you wish for some reason to force MSHTML, you can <code>webview.start(gui='mshtml')</code>.</p> <h3 id="create-window-now-can-load-html-directly"><a href="#create-window-now-can-load-html-directly" class="header-anchor">#</a> create_window now can load html directly</h3> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">import</span> webview
63
64 window <span class="token operator">=</span> webview<span class="token punctuation">.</span>create_window<span class="token punctuation">(</span><span class="token string">'pywebview wow'</span><span class="token punctuation">,</span> html<span class="token operator">=</span><span class="token string">'&lt;html&gt;&lt;body&gt;&lt;h1&gt;pywebview wow!&lt;/h1&gt;&lt;body&gt;&lt;/html&gt;'</span><span class="token punctuation">)</span>
65 webview<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span>
66 </code></pre></div><p>If both url and html parameters are provided, html takes precedence.</p> <h3 id="get-elements"><a href="#get-elements" class="header-anchor">#</a> get_elements</h3> <p>You can now retrieve DOM nodes by using <code>window.get_elements(selector)</code> function. Nodes are serialized using <a href="https://github.com/azaslavsky/domJSON" target="_blank" rel="noopener noreferrer">domJSON<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> library.</p> <p><a href="/3.7/examples/get_elements.html">Example</a></p> <h3 id="config-is-gone"><a href="#config-is-gone" class="header-anchor">#</a> Config is gone</h3> <p><code>webview.config</code> is no more. To set a GUI renderer, use the <code>gui</code> parameter to <code>webview.start</code></p> <h3 id="confirm-quit-is-now-confirm-close"><a href="#confirm-quit-is-now-confirm-close" class="header-anchor">#</a> confirm_quit is now confirm_close</h3> <p>E.g. <code>webview.create_window('Window', confirm_close=True)</code></p> <h1 id="support-the-project"><a href="#support-the-project" class="header-anchor">#</a> Support the project</h1> <p><em>pywebview</em> is a small project with limited resources, any help is welcome. PRs, documentation, research, anything goes. Having said that commits are preferred over comments. Check out the <a href="/contributing">contributing guide</a> to get started.</p> <p>If you find <em>pywebview</em> useful, please support it. We offer donations via Patreon and Open Collective, as well as one-time Paypal donations. If you represent a company, consider becoming a sponsor to get exposure for your company and connect with Python developers.</p> <div class="center spc-l spc-vertical"><a href="https://www.patreon.com/bePatron?u=13226105" data-patreon-widget-type="become-patron-button"><img src="https://c5.patreon.com/external/logo/become_a_patron_button.png" alt="Become a Patron!"></a></div> <div class="center spc-l spc-vertical"><a href="https://opencollective.com/pywebview/donate" target="_blank"><img src="https://opencollective.com/pywebview/donate/[email protected]?color=blue" width="300"></a></div> <div class="center spc-l spc-vertical"><a href="http://bit.ly/2eg2Z5P" target="_blank"><img src="/paypal.png"></a></div></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/r0x0r/pywebview/edit/docs/docs/blog/pywebview3.md" target="_blank" rel="noopener noreferrer">Help us improve this page!</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <!----></footer> <!----> </main></div><div class="global-ui"></div></div>
67 <script src="/3.7/assets/js/app.10a559eb.js" defer></script><script src="/3.7/assets/js/2.0ff7ef1d.js" defer></script><script src="/3.7/assets/js/6.64e1f3ed.js" defer></script>
68 </body>
69 </html>