私がしたいのは、1つのAPIから最初のイベントを表示するだけです。変数は "firstevent"と呼ばれ、値はWebページに表示されます。しかし、最初のイベントはdefの内部にあるので、グローバル変数に変更して、別の関数間で使用できるようにしたいと考えています。しかし、 "NameError:グローバル名 'firstevent'は定義されていません。これは私がやっているものです:それはevents['items'][1]['end']
グローバル変数とPythonフラスコ
firstevent = 1
ことになって、この変数にランダムな値を送信
グローバル変数を定義
global firstevent
を。
@app.route("/")
def index():
return 'User %s' % firstevent
現在、何が起こっているのかわかりませんが、範囲の問題でしょうか?私は多くの答えをオンラインでチェックしていますが、まだ解決策を見つけることができません。ここで
詳細(全体ではなくコード)がある
import os
# Retrieve Flask, our framework
# request module gives access to incoming request data
import argparse
import httplib2
import os
import sys
import json
from flask import Flask, request
from apiclient import discovery
from oauth2client import file
from oauth2client import client
from oauth2client import tools
from apiclient.discovery import build
from oauth2client.client import OAuth2WebServerFlow
import httplib2
global firstevent
app = Flask(__name__)
def main(argv):
# Parse the command-line flags.
flags = parser.parse_args(argv[1:])
# If the credentials don't exist or are invalid run through the native client
# flow. The Storage object will ensure that if successful the good
# credentials will get written back to the file.
storage = file.Storage('sample.dat')
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = tools.run_flow(FLOW, storage, flags)
# Create an httplib2.Http object to handle our HTTP requests and authorize it
# with our good Credentials.
http = httplib2.Http()
http = credentials.authorize(http)
# Construct the service object for the interacting with the Calendar API.
calendar = discovery.build('calendar', 'v3', http=http)
created_event = calendar.events().quickAdd(calendarId='[email protected]', text='Appointment at Somewhere on June 3rd 10am-10:25am').execute()
events = calendar.events().list(calendarId='[email protected]').execute()
#firstevent = events['items'][1]['end']
firstevent = 1
#print events['items'][1]['end']
# Main Page Route
@app.route("/")
def index():
return 'User %s' % firstevent
# Second Page Route
@app.route("/page2")
def page2():
return """<html><body>
<h2>Welcome to page 2</h2>
<p>This is just amazing!</p>
</body></html>"""
# start the webserver
if __name__ == "__main__":
app.debug = True
app.run()
を修正グローバルfirstevent' *外部*任意の関数。それはどのように動作するのではありません。 – kindall