-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
66 lines (41 loc) · 1.67 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import pandas as pd
from flask import Flask, jsonify
from flask_cors import CORS
from google.oauth2.service_account import Credentials
from googleapiclient.discovery import build
import plotly.express as px
import numpy as np
SERVICE_ACCOUNT_FILE = 'google project api json file location'
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
creds = Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
SAMPLE_SPREADSHEET_ID = 'spreadsheet end point'
# service = build('sheets', 'v4', credentials=creds)
# spreadsheet = service.spreadsheets().get(spreadsheetId=SAMPLE_SPREADSHEET_ID).execute()
# sheets = spreadsheet.get('sheets', [])
def fetch_sheet_data():
service = build('sheets', 'v4', credentials=creds)
spreadsheet = service.spreadsheets().get(spreadsheetId=SAMPLE_SPREADSHEET_ID).execute()
sheets = spreadsheet.get('sheets', [])
for sheet in sheets:
title = sheet.get('properties', {}).get('title')
range = f'{title}'
result = service.spreadsheets().values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID, range=range).execute()
values = result.get('values', {})
if values:
df = pd.DataFrame(values)
df.columns = df.iloc[0]
df = df[1:]
return df
return pd.DataFrame()
app = Flask(__name__)
CORS(app)
@app.route('/get-csv-row-count', methods=['GET'])
def get_csv_row_count():
# df = pd.read_csv('sample.csv') # Replace with your CSV file path
df = fetch_sheet_data()
row_count = df.shape[0]
print(row_count)
return jsonify({'row_count': row_count})
# Run the Flask application
if __name__ == '__main__':
app.run(port=5000)