首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Javascript看不到使用eel暴露的python函数

Javascript看不到使用eel暴露的python函数
EN

Stack Overflow用户
提问于 2020-08-15 01:01:40
回答 1查看 527关注 0票数 1

我使用@eel.expose的方法与文档中的相同,但它不起作用。我试图从pyowm向javascript发送天气信息,但javascript不能识别python函数。下面是我的python代码:

代码语言:javascript
运行
复制
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

和html代码:

代码语言:javascript
运行
复制
<!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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-16 21:33:07

eel.start()必须在代码的末尾,下面是正确的代码:

代码语言:javascript
运行
复制
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))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63417081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档