Skip to content

Commit

Permalink
✨ Add support for Google Cloud service account authentication in BigQ…
Browse files Browse the repository at this point in the history
…uery read function
  • Loading branch information
Marigold committed Dec 13, 2024
1 parent 2876953 commit f67073b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
14 changes: 13 additions & 1 deletion apps/wizard/app_pages/producer_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@

import owid.catalog.processing as pr
import pandas as pd
import pandas_gbq
import plotly.express as px
import streamlit as st
from pandas_gbq import read_gbq
from google.oauth2 import service_account
from st_aggrid import AgGrid, GridUpdateMode, JsCode
from st_aggrid.grid_options_builder import GridOptionsBuilder
from structlog import get_logger

from apps.wizard.utils.components import st_horizontal
from etl.config import GOOGLE_APPLICATION_CREDENTIALS
from etl.snapshot import Snapshot
from etl.version_tracker import VersionTracker

Expand Down Expand Up @@ -70,6 +72,16 @@ def columns_producer(min_date, max_date):
return cols_prod


def read_gbq(*args, **kwargs) -> pd.DataFrame:
if GOOGLE_APPLICATION_CREDENTIALS:
# Use service account
credentials = service_account.Credentials.from_service_account_file(GOOGLE_APPLICATION_CREDENTIALS)
return pandas_gbq.read_gbq(*args, **kwargs, credentials=credentials) # type: ignore
else:
# Use browser authentication.
return pandas_gbq.read_gbq(*args, **kwargs) # type: ignore


@st.cache_data(show_spinner=False)
def get_grapher_views(
date_start: str = MIN_DATE.strftime("%Y-%m-%d"),
Expand Down
3 changes: 3 additions & 0 deletions etl/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ def variable_metadata_url(variable_id):
# Default schema for presentation.grapher_config in metadata. Try to keep it up to date with the latest schema.
DEFAULT_GRAPHER_SCHEMA = "https://files.ourworldindata.org/schemas/grapher-schema.006.json"

# Google Cloud service account path (used for BigQuery)
GOOGLE_APPLICATION_CREDENTIALS = env.get("GOOGLE_APPLICATION_CREDENTIALS")


def enable_bugsnag() -> None:
if BUGSNAG_API_KEY:
Expand Down

0 comments on commit f67073b

Please sign in to comment.