I currently have a "working" web app through Google Cloud's App Engine. The only thing that is being displayed when I visit my web app is "Welcome to my Python program!" that I have in my index.html file.
I'm trying to display the rest of the Python code that prints some strings regarding time. What I have in my files isn't working and I'm not sure what I am doing wrong. I've tried messing around with the Python code as I believe that is the issue. Maybe I'm not placing the call to the function in the right place?
Here is what I have as far as files go:
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>
CodePudding user response:
Try changing the last statement in all_about_time
to:
if 930 <= dt_east <= 1600 and (day_of_week != 'Saturday' and day_of_week != 'Sunday'):
return('open')
else:
return('closed')
Then change the following:
@app.route("/")
def hello():
status = all_about_time()
return render_template('index.html', status=status)
Then change 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>
I encourage to read Flask's (excellent) documentation to understand how it works.
I've not run your code but, assuming that all_about_time
time works, it prints a string to (standard) output. The hello
run invokes all_about_time
(which prints the string) and then (importantly) the function renders the index.html
template which produces the output you observe Welcome to my Python Program!
. Even though all_about_time
printed something, the output went to the console and was not included in the page that was rendered in the browser.
With the minimal changes, I made:
all_about_time
now returns a string (eitheropen
orclosed
).hello
invokesall_about_time
and assigns the result (either
openor
closed) to
status`.status
is then passed to the template to be rendered- The template now includes a variable
{{ status }}
which will be replaced with the actual value ofstatus
i.e. (hopefully)open
orclosed