簡體   English   中英

嘗試將Google表格數據獲取到我的Flask Web應用程序中(我的.json文件在哪里?)

[英]Trying to get google sheets data onto my flask web app (where does my .json file go?)

我正在嘗試設置一個Web應用程序,該應用程序將從Google表格獲取數據並顯示它。 我從twilio的這段視頻中得到了這個想法,甚至使它在本地運行。 現在,我正在嘗試使其將信息顯示到html文檔中,但無法完全在我的服務器上正常工作。 我很確定這與此有關...

json_data = open(os.path.join(static, "getinfo.json"), "r")
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name(json_data, scope)
client = gspread.authorize(creds)
sheet = client.open('hotlistsouthforsyth').sheet1

我只是沒有將我的“ getinfo.json”放在正確的位置?

這是我正在使用的整個代碼...

這是我的__init __.py

from flask import Flask, render_template, url_for, json
import random
import spreadsheet

app = Flask(__name__)

def function_test():
    r = lambda: random.randint(0,255)
    return '#%02X%02X%02X' % (r(),r(),r())

@app.route('/')

def homepage():
    firstName = spreadsheet.getInfo()
    textColor = function_test()
    return render_template("home.html", textColor=textColor)

if __name__ == "__main__":
    app.run()

這是電子表格.py

from flask import Flask, render_template, url_for, json
import gspread
from oauth2client.service_account import ServiceAccountCredentials

json_data = open(os.path.join(static, "getinfo.json"), "r")
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name(json_data, scope)
client = gspread.authorize(creds)
sheet = client.open('hotlistsouthforsyth').sheet1

def getUniId(x):
     myvalue = sheet.cell(x,6).value
     return(str(myvalue))
def getFirstName(x):
     myvalue = sheet.cell(x,2).value
     return(str(myvalue))
def getLastName(x):
     myvalue = sheet.cell(x,3).value
     return(str(myvalue))
def getGender(x):
     myvalue = sheet.cell(x,4).value
     return(str(myvalue))
def getImg(x):
     myvalue = sheet.cell(x,5).value
     return(str(myvalue))
def getScore(x):
     myvalue = sheet.cell(x,6).value
     return(str(myvalue))
def getAll(x):
     getUniId(x)
     getFirstName(x)
     getLastName(x)
     getGender(x)
     getImg(x)
     getScore(x)

嘗試這個:

SITE_ROOT = app.root_path

json_data = open(os.path.join(SITE_ROOT,“ static”,“ getinfo.json”),“ r”)

不管這是否起作用,請找出調試器在IDE中的位置,並在該行上設置一個斷點,以便您可以跟蹤對象的狀態並確定故障所在。 有了特定信息后,就可以輕松地在此處發布並獲得答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM