打印烧瓶中的python返回

时间:2020-05-19 19:42:11

标签: python html flask

我在如何向用户显示python函数返回值方面遇到问题。在函数内部,我从API中提取了一些数据,并希望向用户展示。问题在于,当返回字符串时,未设置任何模板,并且用户看到的只是带有该字符串的空白页。当我return render_template时,我只会得到不带字符串的模板。

我应该怎么做才能将它们都放在模板中? 很抱歉打扰,但我确实认为这将是一个简单的解决方案,但我无法解决它:/。

weather.html

<html lang="en">
<link href="//getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="//getbootstrap.com/examples/jumbotron-narrow/jumbotron-narrow.css" rel="stylesheet">
<link href="/static/css/signup.css" rel="stylesheet">
<script src="/static/js/jquery-3.1.1.js"></script>

<link rel="stylesheet" href="/static/style.css"
type="text/css">
<body>
 <div class="login-screen">
     <div class="header">
       <nav>
         <ul class="nav nav-pills pull-right">
           <li role="presentation" class="active">
             <a href="/logout">Logout</a></li>
         </ul>
       </nav>
     </div>
</body>
</html>

main.py和路线

@app.route("/weather")
def pokaz_pogode():
  url = 'https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=439d4b804bc8187953eb36d2a8c26a02'
  with urllib.request.urlopen(url) as response:
    html = response.read()
  json_weather = json.loads(html)
  temp = json_weather["main"]["temp"]
  temp_c = str(round(float(json_weather["main"]["temp"])-273.15,1))
  press = json_weather["main"]["pressure"]
  hum = json_weather["main"]["humidity"]
  time_sunrise = json_weather["sys"]["sunrise"]
  time_sunrise = json_weather["sys"]["sunrise"]
  time_sunrise = time.strftime("%H:%M", time.localtime(int(time_sunrise)))
  time_sunset = json_weather["sys"]["sunset"]
  time_sunset = time.strftime("%H:%M", time.localtime(int(time_sunset)))
  message = "Today we have: " + str(temp) + " K degrees which is " + str(temp_c) + "Celsius degress, pressure is " + str(press) + ", humidity " + str(hum) + "%. Sunrise is at" + str(time_sunrise) +",and sunset at" + str(time_sunset)
  return render_template("/weather", message = message)

1 个答案:

答案 0 :(得分:0)

您需要执行以下操作:

  • weather.html中,您需要使用从后端传递的{{message}}变量。因此,您的weather.py可能像这样:

    <html lang="en">
    <link href="//getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="//getbootstrap.com/examples/jumbotron-narrow/jumbotron-narrow.css" rel="stylesheet">
    <link href="/static/css/signup.css" rel="stylesheet">
    <script src="/static/js/jquery-3.1.1.js"></script>
    
    <link rel="stylesheet" href="/static/style.css"
    type="text/css">
    <body>
     <div class="login-screen">
         <div class="header">
           <nav>
             <ul class="nav nav-pills pull-right">
               <li role="presentation" class="active">
                 <a href="/logout">Logout</a></li>
             </ul>
           </nav>
         </div>
         {{message}}  <!-- this is how to use the message variable  -->
    </body>
    </html>
    
    • main.py中,您需要渲染weather.html页面。您可以通过将render_template更改为以下方式来做到这一点:
    return render_template("weather.html", message=message)  #<-- change `weather` to `weather.html`