forked from daattali/beautiful-jekyll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
71 lines (59 loc) · 1.97 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
from flask import Flask, render_template, request
import sqlite3
from google.oauth2.service_account import Credentials
from googleapiclient.discovery import build
app = Flask(__name__)
# Google Sheets setup
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
SERVICE_ACCOUNT_FILE = 'carbon-calc-411303-9b3d7cf41f73.json' # Replace with your path
SPREADSHEET_ID = '18nBR3dxfyJm9xUC6TjjOdBzCzKTMrbwE6FHPnZDjfEQ' # Replace with your Spreadsheet ID
creds = None
creds = Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
def save_to_sheet(data):
service = build('sheets', 'v4', credentials=creds)
sheet = service.spreadsheets()
values = [data] # Data should be a list
body = {
'values': values
}
result = sheet.values().append(
spreadsheetId=SPREADSHEET_ID,
range="Sheet1", # Update with your sheet name
valueInputOption="USER_ENTERED",
body=body).execute()
# Database setup
def init_db():
conn = sqlite3.connect('users.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL
)
''')
conn.commit()
conn.close()
# Initialize the database
init_db()
@app.route('/')
def calc():
return render_template('calc.html')
@app.route('/submit', methods=['POST'])
def calculate_footprint():
# Extracting data from form
name = request.form.get('name')
email = request.form.get('email')
# Insert data into the database
conn = sqlite3.connect('users.db')
cursor = conn.cursor()
cursor.execute('INSERT INTO users (name, email) VALUES (?, ?)', (name, email))
conn.commit()
conn.close()
# Save to Google Sheets
save_to_sheet([name, email])
# Returning the results
return render_template('results.html', name=name, email=email)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=81, debug=True)