I have a calendar I created from Google Sheets using an API. I want to create another page(route) to display the html code (not the rendered template) of the calendar. Here is my code:
from googleapiclient.discovery import build
from google.oauth2.credentials import Credentials
from google.oauth2 import service_account
from flask import Flask
from flask import Flask, flash, jsonify, redirect, render_template, request, url_for,
session
import requests
app = Flask(__name__)
SERVICE_ACCOUNT_FILE = 'keys.json'
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
creds = None
creds = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
# The ID and range of a sample spreadsheet.
SAMPLE_SPREADSHEET_ID = 'removed for security'
service = build('sheets', 'v4', credentials=creds)
# Call the Sheets API
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
range="new!A2:D9").execute()
header = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
range="new!A1:D1").execute()
request = sheet.values().update(spreadsheetId=SAMPLE_SPREADSHEET_ID,
range="new!A10", valueInputOption="USER_ENTERED", body=
{"values":newvalues}).execute()
@app.route('/', methods=["GET", "POST"])
def index():
values = result.get('values', [])
headers = header.get('values', [])
return render_template("index.html", values=values, headers=headers)
@app.route('/text', methods=["GET", "POST"])
def text():
t = requests.get('http://127.0.0.1:5000')
return (t.content)
if __name__ == '__main__':
app.run()
The '/text' route displays the page exactly like the '/' page. I want it do only return the html code not the rendered template.
Try setting the mime type of the response to text/plain
using make_response
:
from flask import app, make_response, render_template
app = Flask(__name__)
@app.route('/')
def index():
resp = make_response(render_template('template.html'))
resp.mimetype = 'text/plain'
return resp
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.