pythonmacosmacos-catalinapywebview

the javascript of pywebview python library for MacOS is working with a local implementation, but not from a remote web server


I'm quite a noob at any development at all. So I apologize ahead of time if I'm missing something very trivial.

I implemented pywebview on ubuntu and on raspbian, and now I'm trying to do it on MacOS. the test bed is Catalina 10.15.5.

the code I'm running:

import os
import webview
   
def get_ip(self):
            raw_ip = os.popen('curl https://ipinfo.io/ip').read()
            local_ip = raw_ip.replace('\n','')
            response = {"ip": local_ip}
            return response
window = webview.create_window('PyWebView Test', 'https://website.com/index.html', js_api=api, width=1024, height=768)

on this html page, I try to display the public IP of my local machine:

so in the html I run is:

<html>
<body>
<h2 class="text">Current IP:</h2>
<h2 class="text" id="local_ip"></h2>
</body>
<script>
    $(function getipaddress(){
    $(document).ready(function () {
      pywebview.api.get_ip().then(function(response) {
        window.local_ip = response.ip;
        $('#local_ip').html(window.local_ip);
      })
     });
    });
</script>
</html>

when I run this code locally, that is

window = webview.create_window('PyWebView Test', './index.html', js_api=api, width=1024, height=768)

when index.html is in the same folder, I'm able to see the IP address displayed, which is what is desired.

I scoured for a solution, I've found this which seems close, but I don't know how to implement this fix. but unable to find what I need, please help!


Solution

  • within the JS on the html side, added an on mouse movement function and worked, which was acceptable for my use case.