{"id":146,"date":"2023-01-22T08:32:37","date_gmt":"2023-01-21T23:32:37","guid":{"rendered":"https:\/\/global-hack.com\/blog\/?p=146"},"modified":"2023-01-22T08:32:37","modified_gmt":"2023-01-21T23:32:37","slug":"%e3%80%90pywebview%e3%80%91javascript%e3%81%a7python%e5%81%b4%e3%81%aeapi%e3%82%92%e5%91%bc%e3%81%b3%e5%87%ba%e3%81%99","status":"publish","type":"post","link":"https:\/\/global-hack.com\/blog\/archives\/146","title":{"rendered":"\u3010pywebview\u3011javascript\u3067Python\u5074\u306eAPI\u3092\u547c\u3073\u51fa\u3059"},"content":{"rendered":"<h3>\u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\u306e\u30a4\u30d9\u30f3\u30c8\u3092\u30d0\u30c3\u30af\u30a8\u30f3\u30c9\u3067\u53d7\u3051\u53d6\u3063\u3066\u51e6\u7406\u3092\u3059\u308b<\/h3>\n<p>ajax\u901a\u4fe1\u3084fetch\u306a\u3069\u3001\u3084\u308a\u65b9\u306f\u69d8\u3005\u3042\u308b\u3068\u306f\u601d\u3044\u307e\u3059\u304c\u4eca\u56de\u306f<a href=\"https:\/\/pywebview.flowrl.com\/\" target=\"_blank\" rel=\"nofollow noopener\" title=\"pywebview\u516c\u5f0f\u30b5\u30a4\u30c8\">pywebview<\/a>\u3068\u3044\u3046\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u7528\u3044\u3066\u3001javascript\u5074\u306e\u30a4\u30d9\u30f3\u30c8\u3092\u30c8\u30ea\u30ac\u30fc\u306b\u3057\u3066Python\u5074\u306e\u51e6\u7406\u3092\u8d70\u3089\u305b\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p><!--more--><\/p>\n<h3>pywebview\u306e\u958b\u767a\u74b0\u5883\u3092\u7528\u610f\u3059\u308b<\/h3>\n<p>\u307e\u305a\u306f\u4e0b\u8a18\u30ea\u30f3\u30af\u3092\u53c2\u8003\u306b\u3057\u3066\u3001Python\u306e\u958b\u767a\u74b0\u5883\u3092\u7528\u610f\u3057\u307e\u3059\u3002<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"V4uvdmFG2m\"><p><a href=\"https:\/\/global-hack.com\/blog\/archives\/93\">\u3010\u7c21\u5358Python\u3011venv\u3067\u30b5\u30af\u30c3\u3068\u4eee\u60f3\u74b0\u5883\u3092\u7528\u610f\u3057\u3066\u307f\u308b\u3010Mac\u958b\u767a\u74b0\u5883\u69cb\u7bc9\u3011<\/a><\/p><\/blockquote>\n<p><iframe class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;\u3010\u7c21\u5358Python\u3011venv\u3067\u30b5\u30af\u30c3\u3068\u4eee\u60f3\u74b0\u5883\u3092\u7528\u610f\u3057\u3066\u307f\u308b\u3010Mac\u958b\u767a\u74b0\u5883\u69cb\u7bc9\u3011&#8221; &#8212; Global Hack \u30a8\u30f3\u30b8\u30cb\u30a2\u5099\u5fd8\u9332\" src=\"https:\/\/global-hack.com\/blog\/archives\/93\/embed#?secret=4J6dBuaJQr#?secret=V4uvdmFG2m\" data-secret=\"V4uvdmFG2m\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p>\u4e0a\u8a18\u30ea\u30f3\u30af\u5148\u306e\u300c\u4eee\u60f3\u74b0\u5883\u3092\u8d77\u52d5\u3055\u305b\u308b\u300d\u9805\u76ee\u307e\u3067\u9032\u3093\u3060\u3089\u3001\u3053\u3061\u3089\u306e\u8a18\u4e8b\u306e\u4e0b\u3078\u9032\u307f\u307e\u3059\u3002<\/p>\n<p>\u4eca\u56de\u4f5c\u6210\u3057\u305f\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u540d\u306f\u300cpytest\u300d\u3067\u3001\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u540d\u306f\u300capp\u300d\u3068\u306a\u3063\u3066\u304a\u308a\u307e\u3059\u3002\u307e\u305f\u3001\u4eee\u60f3\u74b0\u5883\u8d77\u52d5\u5f8c\u306bapp\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u5185\u306b\u300capp.py\u300d\u3068\u300cindex.html\u300d\u306e\uff12\u3064\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u7528\u610f\u3057\u307e\u3057\u305f\u3002<\/p>\n<h4>\u4e0b\u8a18\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u69cb\u6210\u53c2\u7167<\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/global-hack.com\/blog\/wp-content\/uploads\/2023\/01\/pytest_dir_pywebview.png\" alt=\"pywebview\u306e\u958b\u767a\u74b0\u5883\u5185\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u69cb\u6210\" width=\"303\" height=\"186\" class=\"alignnone size-full wp-image-147\" \/><\/p>\n<h3>pywebview\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n<p>\u4f5c\u6210\u3057\u305f\u4eee\u60f3\u74b0\u5883\u5185\uff08\u3053\u3053\u3067\u306fapp\uff09\u3067\u4e0b\u8a18\u30b3\u30de\u30f3\u30c9\u306b\u3066pywebview\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>(app) $ pip install pywebview\r\n<\/code><\/pre>\n<\/div>\n<h3>javascript\u542b\u3080index.html\u306e\u30b3\u30fc\u30c9<\/h3>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-html\" data-lang=\"HTML\"><code>&lt;html&gt;\r\n\t&lt;head&gt;\r\n\t\t&lt;title&gt;pywebview TEST&lt;\/title&gt;\r\n\t&lt;\/head&gt;\r\n\t&lt;body&gt;\r\n\t\t&lt;h1&gt;\u3010pywebview\u3011javascript\u3067Python\u5074\u306eAPI\u3092\u547c\u3073\u51fa\u3059&lt;\/h1&gt;\r\n\t\t&lt;button id=\"click_btn\"&gt;Button&lt;\/button&gt;\r\n    &lt;div id=\"append_area\"&gt;&lt;\/div&gt;\r\n\t&lt;\/body&gt;\r\n\t&lt;script&gt;\r\n\t\twindow.addEventListener(\"pywebviewready\", function() {\r\n\t\t\tdocument.getElementById(\"click_btn\").addEventListener(\"click\", function(){\r\n\t\t\t\tpywebview.api.strAppend();\r\n\t\t\t})\r\n\t\t})\r\n\t&lt;\/script&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<\/div>\n<p>Button\u3092\u30af\u30ea\u30c3\u30af\u3057\u305f\u3089Python\u5074\u3092\u547c\u3073\u51fa\u3059\u3002<\/p>\n<h3>\u547c\u3073\u51fa\u3055\u308c\u305fPython\u5074\u306e\u30b3\u30fc\u30c9<\/h3>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import webview\r\n\r\nclass BrowserApi:\r\n    def __init__(self):\r\n        self.window = None\r\n\r\n    def strAppend(self):\r\n        self.window.evaluate_js(\r\n            r\"\"\"\r\n            var add_elem = document.createElement('p');\r\n            add_elem.textContent = 'Hello world!';\r\n            document.getElementById(\"append_area\").appendChild(add_elem);\r\n            \"\"\"\r\n            )\r\n\r\ndef threadFunction(window):\r\n    print(window)\r\n\r\ndef main():\r\n    with open(\"app\/index.html\", \"r\", encoding=\"utf-8\") as f:\r\n        html = f.read()\r\n\r\n    BA = BrowserApi()\r\n\r\n    BA.window = webview.create_window(title   = \"Test App\",\r\n                                       html   = html,\r\n                                       js_api = BA,\r\n                                       width  = 1440,\r\n                                       height = 900)\r\n\r\n    webview.start(threadFunction, BA.window, debug=True)\r\n\r\nif __name__ == \"__main__\":\r\n    main()<\/code><\/pre>\n<\/div>\n<p>\u300c<code>strAppend<\/code>\u300d\u30e1\u30bd\u30c3\u30c9\u304c\u547c\u3073\u51fa\u3055\u308c\u3066\u300c<code>self.window.evaluate_js<\/code>\u300d\u306e\u3068\u3053\u308d\u66f8\u304b\u308c\u3066\u3044\u308bjavascript\u306e\u30b3\u30fc\u30c9\u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<h3>javascript\u306e\u30a4\u30d9\u30f3\u30c8\u5b9f\u884c\u5f8c\u306ePython\u5074\u306e\u51e6\u7406\u306e\u7d50\u679c<\/h3>\n<p>\u4e0b\u8a18\u753b\u50cf\u306e\u3088\u3046\u306b\u30dc\u30bf\u30f3\u62bc\u4e0b\u5f8c\u3001\u6587\u5b57\u5217\u304c\u633f\u5165\u3055\u308c\u7d9a\u3051\u3070\u5b8c\u6210\u3067\u3059\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/global-hack.com\/blog\/wp-content\/uploads\/2023\/01\/pywebview_python_javascript_append.png\" alt=\"pywebview\u306b\u3066javascript\u30a4\u30d9\u30f3\u30c8\u5b9f\u884c\u5f8c\u306epython\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u51e6\u7406\" width=\"788\" height=\"435\" class=\"alignnone size-full wp-image-148\" \/><\/p>\n<p>\u4e0a\u8a18\u306e\u3088\u3046\u306a\u8981\u9818\u3067\u3001\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3084\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u306a\u3069\u306e\u9032\u6357\u72b6\u6cc1\u3092\u30d7\u30ed\u30b0\u30ec\u30b9\u30d0\u30fc\u306b\u30d1\u30fc\u30bb\u30f3\u30c8\u8868\u793a\u3059\u308b\u969b\u306b\u7528\u3044\u305f\u3089\u30e6\u30fc\u30b6\u30fc\u30d3\u30ea\u30c6\u30a3\u304c\u5411\u4e0a\u3057\u305d\u3046\u3067\u3059\u306d\u3002<\/p>\n<p>\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u306e\u304c\u9762\u5012\u306a\u65b9\u7528\u306b\u3001\u4e0b\u8a18github\u306b\u5168\u3066\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u30a2\u30c3\u30d7\u3057\u307e\u3057\u305f\u306e\u3067\u3001\u79d2\u3067\u4eca\u56de\u306e\u72b6\u614b\u306b\u307e\u3067\u6301\u3063\u3066\u3044\u3051\u307e\u3059\u3002<\/p>\n<p><a href=\"https:\/\/github.com\/Global-Hack\/pywebview_BrowserApi\" target=\"_blank\" rel=\"nofollow noopener\" title=\"pywebview\u3067javascript\u3068python\u306e\u52d5\u7684\u306a\u9023\u643a\">https:\/\/github.com\/Global-Hack\/pywebview_BrowserApi<\/a><\/p>\n<p>\u5148\u306b\u3053\u308c\u3092\u8cbc\u3089\u3093\u304b\u3044\uff08\u7b11\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>\u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\u306e\u30a4\u30d9\u30f3\u30c8\u3092\u30d0\u30c3\u30af\u30a8\u30f3\u30c9\u3067\u53d7\u3051\u53d6\u3063\u3066\u51e6\u7406\u3092\u3059\u308b ajax\u901a\u4fe1\u3084fetch\u306a\u3069\u3001\u3084\u308a\u65b9\u306f\u69d8\u3005\u3042\u308b\u3068\u306f\u601d\u3044\u307e\u3059\u304c\u4eca\u56de\u306fpywebview\u3068\u3044\u3046\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u7528\u3044\u3066\u3001javascript\u5074\u306e\u30a4\u30d9\u30f3\u30c8\u3092\u30c8\u30ea\u30ac\u30fc\u306b\u3057\u3066Python\u5074\u306e\u51e6\u7406 <a class=\"mh-excerpt-more\" href=\"https:\/\/global-hack.com\/blog\/archives\/146\" title=\"\u3010pywebview\u3011javascript\u3067Python\u5074\u306eAPI\u3092\u547c\u3073\u51fa\u3059\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":149,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[94,46,91],"tags":[92,93,95,96],"class_list":["post-146","post","type-post","status-publish","format-standard","has-post-thumbnail","category-javascript","category-python","category-pywebview","tag-ajax","tag-fetch","tag-pywebviewready","tag-webview"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/global-hack.com\/blog\/wp-content\/uploads\/2023\/01\/python_javascript_pywebview_demo.png","_links":{"self":[{"href":"https:\/\/global-hack.com\/blog\/wp-json\/wp\/v2\/posts\/146","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/global-hack.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/global-hack.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/global-hack.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/global-hack.com\/blog\/wp-json\/wp\/v2\/comments?post=146"}],"version-history":[{"count":0,"href":"https:\/\/global-hack.com\/blog\/wp-json\/wp\/v2\/posts\/146\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/global-hack.com\/blog\/wp-json\/wp\/v2\/media\/149"}],"wp:attachment":[{"href":"https:\/\/global-hack.com\/blog\/wp-json\/wp\/v2\/media?parent=146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/global-hack.com\/blog\/wp-json\/wp\/v2\/categories?post=146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/global-hack.com\/blog\/wp-json\/wp\/v2\/tags?post=146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}