Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load data from Google Sheets #35

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@ target/
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
Expand Down
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.8.1
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
# viveros
Prototipo de proyecto viveros para la Feria de la Transparencia 2019.

# Getting Started

## Pre-requisites
- ```MongoDB```:leaves: - [Installation guide](https://docs.mongodb.com/manual/installation/)
- ```pyenv``` [It is optional, but would improve your development experience](https://github.com/pyenv/pyenv)
- ```Python 3.8.1```:snake:

## Installation
```pip install -r requirements.txt```

## Running
```python app.py```
8 changes: 7 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
from dotenv import load_dotenv
load_dotenv()

from flask import Flask
from flask import render_template
import qrcode

from gsheet_utils import GSheet

from io import BytesIO
import base64

Expand All @@ -16,10 +20,12 @@
app = Flask(__name__)
donations = db_connect_to_collection(MONGO_URI, 'viveros', 'donaciones')

gsheet = GSheet()

@app.route('/')
def index():
return render_template('index.html')
total_donations = gsheet.get_data("'donaciones 2020'!I1")[0][0]
return render_template('index.html', total_donations=f"{int(total_donations):,}")


@app.route('/dashboard')
Expand Down
3 changes: 2 additions & 1 deletion dbutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
# -*- coding: utf-8 -*-

from pymongo import MongoClient
import os
# import pandas as pd

# Access to MongoDB remote cluster:
MONGO_URI = ""
MONGO_URI = os.getenv("MONGO_URI")


def db_connect_to_collection(MONGO_URI, database, collection):
Expand Down
43 changes: 43 additions & 0 deletions gsheet_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import json
import googleapiclient.discovery
from google.oauth2 import service_account
import os


GOOGLE_SPREADSHEET_ID = os.getenv("GOOGLE_SPREADSHEET_ID")

class GSheet:
google_credentials_json = os.getenv("GOOGLE_CREDENTIALS_JSON")
gkeys = json.loads(google_credentials_json)

def __init__(self):
self.service = self.__get_service()

def __get_credentials(self):
scopes = ["https://www.googleapis.com/auth/spreadsheets.readonly"]
GOOGLE_PRIVATE_KEY = self.gkeys["private_key"]
# The environment variable has escaped newlines, so remove the extra backslash
GOOGLE_PRIVATE_KEY = GOOGLE_PRIVATE_KEY.replace('\\n', '\n')

account_info = {
"private_key": GOOGLE_PRIVATE_KEY,
"client_email": self.gkeys["client_email"],
"token_uri": "https://accounts.google.com/o/oauth2/token",
}

credentials = service_account.Credentials.from_service_account_info(account_info, scopes=scopes)
return credentials

def __get_service(self):
service_name='sheets'
api_version='v4'
credentials = self.__get_credentials()
service = googleapiclient.discovery.build(service_name, api_version, credentials=credentials)
return service

def get_data(self, range_name):
result = self.service.spreadsheets().values().get(
spreadsheetId=GOOGLE_SPREADSHEET_ID, range=range_name
).execute()
values = result.get('values', [])
return values
10 changes: 7 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
Flask
pymongo[srv]
qrcode
flask
google-api-python-client
google-auth
google-auth-httplib2
gunicorn
pymongo
python-dotenv
qrcode
Empty file modified start.sh
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<div class="col-md-8 ml-auto mr-auto">
<div class="brand">
<div class="odometer">
<h1 class="scroller" style="font-size: 8em;">19,104</h1>
<h1 class="scroller" style="font-size: 8em;">{{total_donations}}</h1>
<h3><i class="material-icons">nature</i> Árboles y plantas donadas</h3>
</div>
</div>
Expand Down