import functions_framework from flask import Flask, jsonify, send_file from flask_cors import CORS import matplotlib.pyplot as plt from matplotlib.backends.backend_svg import FigureCanvasSVG import io import os from firebase_admin import initialize_app, firestore import datetime import pandas as pd from google.cloud.firestore import FieldFilter app = Flask(__name__) CORS(app) initialize_app() def create_svg_plot(): db = firestore.client() test_collection = db.collection("testData") data = [] current_datetime = datetime.datetime.now() start_datetime = current_datetime.replace(hour=0, minute=0, second=0, microsecond=0) start_date = start_datetime.strftime('%Y-%m-%d') end_datetime = start_datetime + datetime.timedelta(days=1) end_date = end_datetime.strftime('%Y-%m-%d') test_docs = test_collection.where(filter=FieldFilter('testTime', '>=', start_date)).where(filter=FieldFilter('testTime', '<', end_date)).stream() for test_doc in test_docs: test_data = test_doc.to_dict() data.append(test_data) df = pd.DataFrame(data) fig, ax = plt.subplots() box_plot = df.boxplot(column='deviceRatio', by='classificationResult', vert=True, ax=ax) # plt.title('Box Plot of Device Ratio by Classification Result') plt.xlabel('Classification Result') plt.ylabel('Device Ratio') svg_output = io.StringIO() canvas = FigureCanvasSVG(fig) canvas.print_svg(svg_output) plt.close(fig) return svg_output.getvalue() @functions_framework.http def crdboxplot(request): """HTTP Cloud Function. Args: request (flask.Request): The request object. Returns: The response text, or any set of values that can be turned into a Response object using `make_response` . """ if request.method == 'OPTIONS': headers = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type', # 'Access-Control-Max-Age': '3600' } return ('', 204, headers) headers = { 'Access-Control-Allow-Origin': '*' } svg_plot = create_svg_plot() return send_file(io.BytesIO(svg_plot.encode()), as_attachment=False, download_name="plot.svg", mimetype='image/svg+xml')