【问题标题】:Google Cloud web app not displaying custom Python code谷歌云网络应用不显示自定义 Python 代码
【发布时间】:2022-01-17 03:49:40
【问题描述】:

我目前通过 Google Cloud 的 App Engine 有一个“工作”网络应用程序。 当我访问我的网络应用程序时,唯一显示的是“欢迎使用我的 Python 程序!”我的 index.html 文件中有。

我正在尝试显示打印一些关于时间的字符串的其余 Python 代码。 我的文件中的内容不起作用,我不确定我做错了什么。我已经尝试过使用 Python 代码,因为我认为这是问题所在。也许我没有在正确的地方调用函数?

就文件而言,这是我所拥有的:

requirements.txt

Flask==2.0.2
pytz==2020.1
gunicorn==19.3.0

app.yaml

runtime: python
env: flex
entrypoint: gunicorn -b :$PORT main:app

runtime_config:
    python_version: 3

handlers:
- url: /static
  static_dir: static

- url: /.*
  script: auto

manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10

main.py

import pytz
from datetime import datetime
from flask import Flask, render_template

app = Flask(__name__)

def all_about_time():
    #Prints local date.
    current_time = datetime.now()
    local_date = datetime.strftime(current_time, '%b %d, %Y')
    print('Today\'s date is: '  + str(local_date))
        
    #Prints out the time on the east coast. Helps give context on market hours.    
    eastern_time = datetime.strftime(current_time.astimezone(pytz.timezone('US/Eastern')), '%I:%M %p')
    print('Time on the East Coast is currently: ' + eastern_time)
    
    #This logic block dictates whether the market is closed or open. So far does not account for holidays.
    day_of_week = datetime.strftime(current_time.astimezone(pytz.timezone('US/Eastern')), '%A')
    dt_east = int(datetime.strftime(current_time.astimezone(pytz.timezone('US/Eastern')), '%H%M'))
    if 930 <= dt_east <= 1600 and (day_of_week != 'Saturday' and day_of_week != 'Sunday'):
        print('The market is open!')
    else:
        print('The market is closed.')

@app.route("/")
def hello():
    all_about_time()
    return render_template('index.html')

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="{{ url_for('static', filename='script.js') }}"></script>
    <link type="text/css" rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MyPythonProgram</title>
</head>
<body>
    <h1>Welcome to my Python Program!</h1>
</body>
</html>

【问题讨论】:

    标签: python flask google-cloud-platform web-applications


    【解决方案1】:

    尝试将all_about_time 中的最后一条语句更改为:

        if 930 <= dt_east <= 1600 and (day_of_week != 'Saturday' and day_of_week != 'Sunday'):
            return('open')
        else:
            return('closed')
    

    然后更改以下内容:

    @app.route("/")
    def hello():
        status = all_about_time()
        return render_template('index.html', status=status)
    

    那就换index.html:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <script src="{{ url_for('static', filename='script.js') }}"></script>
        <link type="text/css" rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto">
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>MyPythonProgram</title>
    </head>
    <body>
        <h1>The market is {{ status }}</h1>
    </body>
    </html>
    

    我鼓励阅读 Flask(优秀)documentation 以了解其工作原理。

    我没有运行您的代码,但假设 all_about_time 时间有效,它会打印一个字符串到(标准)输出。 hello 运行调用 all_about_time(打印字符串)和 then(重要的是)该函数呈现 index.html 模板,该模板产生您观察到的输出 Welcome to my Python Program!。即使all_about_time 打印了一些内容,输出也会进入控制台,并且不包含在浏览器中呈现的页面中。

    改动很小:

    1. all_about_time 返回一个字符串(open 或 closed)。
    2. hello 调用 all_about_time 并将结果(open 或 closed)分配给 status。
    3. 然后将status 传递给要渲染的模板
    4. 模板现在包含一个变量{{ status }},它将替换为status 的实际值,即(希望)open 或closed

    【讨论】:

    • 谢谢你。我会尝试这些编辑。我那里的其他打印语句呢?感谢您提供文档链接。
    • 不客气!请留下其他print 语句以证明这一点。如果您在本地运行 Flask 服务器,您将看到 print 语句的输出转到服务器的输出(而不是浏览器页面)。如果你愿意,你可以:python3 if 930 &lt;= dt_east &lt;= 1600 and (day_of_week != 'Saturday' and day_of_week != 'Sunday'): status = 'open' else: status = 'closed' print('The market is {status}'.format(status=status)) return status
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多