我目前通過 Google Cloud 的 App Engine 有一個“有效”的網路應用程式。當我訪問我的網路應用程式時,唯一顯示的是“歡迎使用我的 Python 程式!” 我的 index.html 檔案中有。
我正在嘗試顯示列印一些有關時間的字串的其余 Python 代碼。我的檔案中的內容不起作用,我不確定我做錯了什么。我試過搞亂 Python 代碼,因為我認為這就是問題所在。也許我沒有把對函式的呼叫放在正確的位置?
就檔案而言,這是我所擁有的:
要求.txt
Flask==2.0.2
pytz==2020.1
gunicorn==19.3.0
應用程式.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
主檔案
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')
索引.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>
uj5u.com熱心網友回復:
嘗試將最后一個陳述句更改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 的(優秀的)檔案以了解它是如何作業的。
我沒有運行你的代碼,但是,假設all_about_time時間有效,它會向(標準)輸出列印一個字串。在hello運行所呼叫all_about_time(其中列印字串)和然后(重要)的功能呈現index.html產生你觀察輸出模板Welcome to my Python Program!。即使all_about_time列印了一些東西,輸出也會進入控制臺,并且沒有包含在瀏覽器中呈現的頁面中。
更改最少:
all_about_time回傳一個字串(open或closed)。hello呼叫all_about_time并將結果(open或closed)分配給status。status然后傳遞給要渲染的模板- 模板現在包含一個變數
{{ status }},該變數將替換為statusie(希望如此)open或的實際值closed
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/386201.html
上一篇:FlaskAPI請求回傳舊錯誤
下一篇:Spring@RequestBodyMapping將所有屬性映射到來自cleancamelCasePOSTPostman請求的空值
