简体   繁体   中英

Javascript can`t see python function exposed with eel

I used @eel.expose the same way as in the documentation, but it doesnt work. I am trying to send information about weather from pyowm to javascript, but javascript doesnt recognize python function. Here is my python code:

import eel
import pyowm

owm = pyowm.OWM("78e8414197f287eb489f857bf10fa96a")

eel.init("web")
eel.start("main.html", size=(700, 700))


@eel.expose
def getWeather(place):
    mgr = owm.weather_manager()
    observation = mgr.weather_at_place(place)
    w = observation.weather

    temp = w.temperature('celsius')['temp']

    return temp

And html code:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Weather</title>
        <script type="text/javascript" src="/eel.js"></script>
        <link rel="stylesheet" type="text/css" href="main.css">
    </head>
    <body>
        <input type="text" id="location" placeholder="Enter city and country" required="" value="New York, USA">
        <button id="getWeatherButton">GET WEATHER</button>
        <div id="result"></div>
        
        <script type="text/javascript">
            document.getElementById("getWeatherButton").onclick = async function displayWeather() {
                let place = document.getElementById("location").value;
                let temp = await eel.getWeather(place)();

                document.getElementById("result").innerHTML = temp;
            };
        </script>
    </body>
</html>

eel.start() has to be in the end of the code Here is right code:

import eel
import pyowm

owm = pyowm.OWM("78e8414197f287eb489f857bf10fa96a")

eel.init("web")


@eel.expose
def getWeather(place):
    mgr = owm.weather_manager()
    observation = mgr.weather_at_place(place)
    w = observation.weather

    temp = w.temperature('celsius')['temp']

    return temp


eel.start("main.html", size=(700, 700))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM