liveBook · Manning

BigQuery API

As explained in Build Python Web Apps with Streamlit

A BigQuery client is a programmatic client object that provides methods and abstractions for interacting with BigQuery.

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

How Build Python Web Apps with Streamlit covers it

How deep it goes
Explains API enablement, service-account authentication, client construction, table naming, SQL execution, and dataframe conversion, but not advanced SQL.
Examples
Uses a Streamlit CEO dashboard, a sales_data table, a fully qualified table name, and a SELECT * query.
What it assumes
Assumes a Python application, Streamlit secrets, and a Google Cloud project; teaches the BigQuery connection and authentication steps.
What you can do after
Build it from scratch by configuring credentials, creating BigQuery clients, querying a table, and returning results as a dataframe.
How it connects
Connects BigQuery clients with service accounts, Streamlit secrets, the BigQuery Storage API, SQL, Pandas dataframes, and separation of concerns.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.