Search code examples
javascriptpythonhtmleel

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>

Solution

  • 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))