A BigQuery client is a Python object that provides methods for connecting to BigQuery, submitting SQL queries, and retrieving table data. A Streamlit app authenticates with a Google Cloud service account, creates the client from stored credentials, and can pass results into a Pandas dataframe. section 7.7.2 section 7.7.3
A practical connection follows these steps:
- Enable the BigQuery API and BigQuery Storage API in the Google Cloud project. section 7.7.2
- Create a service account with project access, generate a JSON key, and store its contents in Streamlit secrets. section 7.7.2
- Install the BigQuery, BigQuery Storage, and db-dtypes packages. section 7.7.3
- Create the client from the secret credentials, construct a project.dataset.table reference, execute SQL, and convert the result to a dataframe. section 7.7.3
Loading a BigQuery table
A data loader can create a client with bigquery.Client.from_service_account_info(service_account_info), run SELECT * against a fully qualified table, wait for the query job, and return result.to_dataframe(bqstorage_client=storage_client). Keeping this work inside load_data preserves the app’s separation between data access and the rest of the dashboard. section 7.7.3
Listing 7.3 data_loader.py reimplemented to use BigQuery
import streamlit as st
from google.cloud import bigquery, bigquery_storage
DATASET = "sia_ceo_dashboard"
TABLE = "sales_data"
def load_data():
service_account_info = st.secrets["bigquery"]
client = bigquery.Client.from_service_account_info(service_account_info)
creds = client._credentials
storage_client = bigquery_storage.BigQueryReadClient(credentials=creds)
project_id = service_account_info["project_id"]
query = f"SELECT * from `{project_id}.{DATASET}.{TABLE}`"
query_job = client.query(query)
result = query_job.result()
return result.to_dataframe(bqstorage_client=storage_client)
Browse the complete data_loader.py implementation, including credentials, storage access, table naming, SQL, and dataframe conversion. section 7.7.3
