{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "k5Ny8fDoMqNE"
},
"source": [
"# Demo Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "aairNG8GMqNG"
},
"source": [
"\n",
"
\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "xouDdMjFMqNH"
},
"source": [
"This is a Google Colab notebook that showcases various features of the [`gspread-models` package](https://github.com/s2t2/gspread-models-py).\n",
"\n",
"In this demo, we take advantage of the simplified Google authentication process within Colab to obtain credentials for the logged in user (instead of using a service account credentials JSON file)."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dVY-MEQMfBKx"
},
"source": [
"## Package Installation"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "iRNmtlPzg4-i"
},
"source": [
"Package installation:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"id": "4AHQ9Raue_bn"
},
"outputs": [],
"source": [
"%%capture\n",
"#!pip uninstall gspread_models\n",
"!pip install git+https://github.com/s2t2/gspread-models-py.git@imports --upgrade\n",
"#!pip install gspread_models==1.0.7"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"id": "qVhM2F2fNOY9"
},
"outputs": [],
"source": [
"!pip list | grep gspread_models"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "sAo-H0FafPVy",
"outputId": "2f08f7f4-15a2-4cea-a3b8-b4e77437c011"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"gspread-models 1.0.7\n"
]
}
],
"source": [
"!pip list | grep gspread-models"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"id": "MAj_N4n513gR"
},
"outputs": [],
"source": [
"#from gspread_models import VERSION\n",
"#print(VERSION)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "e1i24s_Ugx4o"
},
"source": [
"## Google Credentials"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0pFarxnbJskv"
},
"source": [
"Asks you to login via google account:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"id": "HlOn7AgKFh1R"
},
"outputs": [],
"source": [
"from google.colab import auth\n",
"\n",
"auth.authenticate_user()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zM0SZ0moJ38d"
},
"source": [
"Gets credentials for the logged in user:"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 0
},
"id": "Ib7gP85SG3CG",
"outputId": "915b60e0-85cd-434c-9981-81313637950b"
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"
google.auth.compute_engine.credentials.Credentials
def __init__(service_account_email='default', quota_project_id=None, scopes=None, default_scopes=None, universe_domain=None)
/usr/local/lib/python3.10/dist-packages/google/auth/compute_engine/credentials.pyCompute Engine Credentials.\n",
"\n",
"These credentials use the Google Compute Engine metadata server to obtain\n",
"OAuth 2.0 access tokens associated with the instance's service account,\n",
"and are also used for Cloud Run, Flex and App Engine (except for the Python\n",
"2.7 runtime, which is supported only on older versions of this library).\n",
"\n",
"For more information about Compute Engine authentication, including how\n",
"to configure scopes, see the `Compute Engine authentication\n",
"documentation`_.\n",
"\n",
".. note:: On Compute Engine the metadata server ignores requested scopes.\n",
" On Cloud Run, Flex and App Engine the server honours requested scopes.\n",
"\n",
".. _Compute Engine authentication documentation:\n",
" https://cloud.google.com/compute/docs/authentication#using
\n",
" \n",
"
"
],
"text/plain": [
"google.auth.compute_engine.credentials.Credentials"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from google.auth import default\n",
"\n",
"creds, _ = default()\n",
"type(creds)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "TXhcXdUrRAKj"
},
"source": [
"## Google Sheets Setup"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "G3HcQfRQRKCJ"
},
"source": [
"Create a Google Sheets document with \"books\" sheet, with columns: `id`, `title`, `author`, `year`, and `created_at`.\n",
"\n",
"Note the document identifier, and set it as the `GSPREAD_MODELS_DOCUMENT_ID` notebook secret."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "OGjaDr9Vez0e"
},
"source": [
"Use the document identifier from notebook secrets:"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"id": "J4eXoNQHgawN"
},
"outputs": [],
"source": [
"from google.colab import userdata\n",
"\n",
"GOOGLE_SHEETS_DOCUMENT_ID = userdata.get(\"GSPREAD_MODELS_DOCUMENT_ID\") or \"your-document-id-here\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "OG5YBx0CdSYx"
},
"source": [
"## Spreadsheet Service"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ohOcIBbXR3xd"
},
"source": [
"The spreadsheet service provides a lower level interface into the configured google sheet document. You can use it if you like."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "pSQ8SNOkj6h9"
},
"source": [
"Build a new service instance:"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "6mqMJycrgpZC",
"outputId": "cc841f19-a00b-451a-fde3-94104d2064a6"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"SPREADSHEET SERVICE...\n",
"DOCUMENT ID: 1Nk-UBd-3TyWZqbWSvKIIF-S_KKdrkVFRVeNrFy_F1gs\n"
]
}
],
"source": [
"#from gspread_models.service import SpreadsheetService\n",
"from gspread_models import SpreadsheetService\n",
"\n",
"service = SpreadsheetService(creds=creds, document_id=GOOGLE_SHEETS_DOCUMENT_ID)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "MsySNzYWkBak",
"outputId": "536f508e-1620-45e8-fe3c-4d22f68d6f3a"
},
"outputs": [
{
"data": {
"text/plain": [
""
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"service.doc"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "FXBBG3dwj95e",
"outputId": "2ac8a44f-2c8f-4c06-d7d5-b1e64e5b0e51"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Info\n",
"products\n",
"orders\n",
"books\n"
]
}
],
"source": [
"for sheet in service.sheets:\n",
" print(sheet.title)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zGcO45SgdWy6"
},
"source": [
"## Binding the Base Model"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "wM3DWDLwjJz2"
},
"source": [
"Bind the base model with access to your account credentials and your document:"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "TZNcgKtjgIvH",
"outputId": "f12f04f4-d6fc-4eb3-f63b-604bb73bd620"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"SPREADSHEET SERVICE...\n",
"DOCUMENT ID: 1Nk-UBd-3TyWZqbWSvKIIF-S_KKdrkVFRVeNrFy_F1gs\n"
]
}
],
"source": [
"#from gspread_models.base import BaseModel\n",
"from gspread_models import BaseModel\n",
"\n",
"# if you already have the service handy, you can set it:\n",
"#BaseModel.service = service\n",
"\n",
"# otherwise you can bypass the service altogether:\n",
"BaseModel.bind(credentials=creds, document_id=GOOGLE_SHEETS_DOCUMENT_ID)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ZAMpwfwZdb-U"
},
"source": [
"## Defining Child Model Classes"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1094P6NBjTNq"
},
"source": [
"Now, defining custom classes that inherit from the base model:"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"id": "GYTgcrjMfIrA"
},
"outputs": [],
"source": [
"class Book(BaseModel):\n",
"\n",
" SHEET_NAME = \"books\"\n",
"\n",
" COLUMNS = [\"title\", \"author\", \"year\"]"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9L_sHhcsdfzX"
},
"source": [
"## Queries"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "hAGNABrldiRb"
},
"source": [
"Destroy all records:"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "o3mSWe0Wroy8",
"outputId": "77ffcdcb-3f4d-4e6b-a319-f1cbb61663d4"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"GET SHEET ('books')...\n"
]
},
{
"data": {
"text/plain": [
"{'spreadsheetId': '1Nk-UBd-3TyWZqbWSvKIIF-S_KKdrkVFRVeNrFy_F1gs',\n",
" 'replies': [{}]}"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Book.destroy_all()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "kYRFnqBGdkXA"
},
"source": [
"Populate sheet with default records:"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "meLRpTPtcubm",
"outputId": "e3ad9494-dcb9-4028-e1a8-4918cfe893d8"
},
"outputs": [
{
"data": {
"text/plain": [
"{'spreadsheetId': '1Nk-UBd-3TyWZqbWSvKIIF-S_KKdrkVFRVeNrFy_F1gs',\n",
" 'tableRange': 'books!A1:F1',\n",
" 'updates': {'spreadsheetId': '1Nk-UBd-3TyWZqbWSvKIIF-S_KKdrkVFRVeNrFy_F1gs',\n",
" 'updatedRange': 'books!A2:E13',\n",
" 'updatedRows': 12,\n",
" 'updatedColumns': 5,\n",
" 'updatedCells': 60}}"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Book.create_all([\n",
" {\"title\": \"To Kill a Mockingbird\", \"author\": \"Harper Lee\", \"year\": 1960},\n",
" {\"title\": \"1984\", \"author\": \"George Orwell\", \"year\": 1949},\n",
" {\"title\": \"The Great Gatsby\", \"author\": \"F. Scott Fitzgerald\", \"year\": 1925},\n",
" {\"title\": \"The Catcher in the Rye\", \"author\": \"J.D. Salinger\", \"year\": 1951},\n",
" {\"title\": \"Pride and Prejudice\", \"author\": \"Jane Austen\", \"year\": 1813},\n",
" {\"title\": \"To the Lighthouse\", \"author\": \"Virginia Woolf\", \"year\": 1927},\n",
" {\"title\": \"The Hobbit\", \"author\": \"J.R.R. Tolkien\", \"year\": 1937},\n",
" {\"title\": \"Moby-Dick\", \"author\": \"Herman Melville\", \"year\": 1851},\n",
" {\"title\": \"Brave New World\", \"author\": \"Aldous Huxley\", \"year\": 1932},\n",
" {\"title\": \"Alice's Adventures in Wonderland\", \"author\": \"Lewis Carroll\", \"year\": 1865},\n",
" {\"title\": \"Harry Potter and the Philosopher's Stone\", \"author\": \"J.K. Rowling\", \"year\": 1997},\n",
" {\"title\": \"Harry Potter and the Chamber of Secrets\", \"author\": \"J.K. Rowling\", \"year\": 1998},\n",
"])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "w3fb_9_Xdmhi"
},
"source": [
"Fetch all records from the sheet:"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "0ilfQfXkcwmn",
"outputId": "9cc03f2f-4073-454b-e198-c449ffd82d80"
},
"outputs": [
{
"data": {
"text/plain": [
"12"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"books = Book.all()\n",
"len(books)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "q0mnDu7Ic2-Q",
"outputId": "52c86bde-9333-46a7-c13c-7c67e281db3f"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 | To Kill a Mockingbird | Harper Lee\n",
"2 | 1984 | George Orwell\n",
"3 | The Great Gatsby | F. Scott Fitzgerald\n",
"4 | The Catcher in the Rye | J.D. Salinger\n",
"5 | Pride and Prejudice | Jane Austen\n",
"6 | To the Lighthouse | Virginia Woolf\n",
"7 | The Hobbit | J.R.R. Tolkien\n",
"8 | Moby-Dick | Herman Melville\n",
"9 | Brave New World | Aldous Huxley\n",
"10 | Alice's Adventures in Wonderland | Lewis Carroll\n",
"11 | Harry Potter and the Philosopher's Stone | J.K. Rowling\n",
"12 | Harry Potter and the Chamber of Secrets | J.K. Rowling\n"
]
}
],
"source": [
"for book in books:\n",
" print(book.id, book.title, book.author, sep=\" | \")\n",
" # book.created_at.strftime(\"%Y-%m-%d\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "beMrbSlsdvAI"
},
"source": [
"Find record by id:"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ynt7cK9-drjw",
"outputId": "05cbe06b-a193-4648-e3c3-c5db16cf4078"
},
"outputs": [
{
"data": {
"text/plain": [
"{'id': 3,\n",
" 'title': 'The Great Gatsby',\n",
" 'author': 'F. Scott Fitzgerald',\n",
" 'year': 1925,\n",
" 'created_at': datetime.datetime(2024, 5, 28, 21, 22, 56, 732122, tzinfo=datetime.timezone.utc)}"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"book = Book.find(3)\n",
"dict(book)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zXbOGHfGdo3_"
},
"source": [
"Filter records on matching conditions:\n"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Ym0-l6_vd8yi",
"outputId": "8607e7af-2f3e-47b3-e6d6-9317525d202a"
},
"outputs": [
{
"data": {
"text/plain": [
"{'id': 3,\n",
" 'title': 'The Great Gatsby',\n",
" 'author': 'F. Scott Fitzgerald',\n",
" 'year': 1925,\n",
" 'created_at': datetime.datetime(2024, 5, 28, 21, 22, 56, 732122, tzinfo=datetime.timezone.utc)}"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"book = Book.where(author=\"F. Scott Fitzgerald\")[0]\n",
"dict(book)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "2KsqY75aeIJ-",
"outputId": "c327ede5-7b8b-454f-f83a-d247c9eaccbc"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"11 | Harry Potter and the Philosopher's Stone | J.K. Rowling\n",
"12 | Harry Potter and the Chamber of Secrets | J.K. Rowling\n"
]
}
],
"source": [
"hp_books = Book.where(author=\"J.K. Rowling\")\n",
"for book in hp_books:\n",
" print(book.id, book.title, book.author, sep=\" | \")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "kqMsNINQeFJG"
},
"source": [
"## Pandas Support"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "y-5bLjaLPoP9"
},
"source": [
"### Formatting Records as Pandas DataFrame\n",
"\n",
"FYI: It is easy to construct a pandas DataFrame containing the information fetched from the sheet, by converting the records to a list of dictionaries:"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 206
},
"id": "JD7XitraPPsi",
"outputId": "594f9a22-4e41-4eab-fd1b-f91337fc4e0c"
},
"outputs": [
{
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"summary": "{\n \"name\": \"books_df\",\n \"rows\": 12,\n \"fields\": [\n {\n \"column\": \"id\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 3,\n \"min\": 1,\n \"max\": 12,\n \"num_unique_values\": 12,\n \"samples\": [\n 11,\n 10,\n 1\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 12,\n \"samples\": [\n \"Harry Potter and the Philosopher's Stone\",\n \"Alice's Adventures in Wonderland\",\n \"To Kill a Mockingbird\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"author\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 11,\n \"samples\": [\n \"Virginia Woolf\",\n \"Harper Lee\",\n \"Lewis Carroll\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"year\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 56,\n \"min\": 1813,\n \"max\": 1998,\n \"num_unique_values\": 12,\n \"samples\": [\n 1997,\n 1865,\n 1960\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"created_at\",\n \"properties\": {\n \"dtype\": \"date\",\n \"min\": \"2024-05-28 21:22:56.730528+00:00\",\n \"max\": \"2024-05-28 21:22:56.734850+00:00\",\n \"num_unique_values\": 12,\n \"samples\": [\n \"2024-05-28 21:22:56.732391+00:00\",\n \"2024-05-28 21:22:56.732358+00:00\",\n \"2024-05-28 21:22:56.730528+00:00\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}",
"type": "dataframe",
"variable_name": "books_df"
},
"text/html": [
"\n",
" \n",
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" id | \n",
" title | \n",
" author | \n",
" year | \n",
" created_at | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1 | \n",
" To Kill a Mockingbird | \n",
" Harper Lee | \n",
" 1960 | \n",
" 2024-05-28 21:22:56.730528+00:00 | \n",
"
\n",
" \n",
" 1 | \n",
" 2 | \n",
" 1984 | \n",
" George Orwell | \n",
" 1949 | \n",
" 2024-05-28 21:22:56.732074+00:00 | \n",
"
\n",
" \n",
" 2 | \n",
" 3 | \n",
" The Great Gatsby | \n",
" F. Scott Fitzgerald | \n",
" 1925 | \n",
" 2024-05-28 21:22:56.732122+00:00 | \n",
"
\n",
" \n",
" 3 | \n",
" 4 | \n",
" The Catcher in the Rye | \n",
" J.D. Salinger | \n",
" 1951 | \n",
" 2024-05-28 21:22:56.732157+00:00 | \n",
"
\n",
" \n",
" 4 | \n",
" 5 | \n",
" Pride and Prejudice | \n",
" Jane Austen | \n",
" 1813 | \n",
" 2024-05-28 21:22:56.732190+00:00 | \n",
"
\n",
" \n",
"
\n",
"
\n",
"
\n",
"
\n"
],
"text/plain": [
" id title author year \\\n",
"0 1 To Kill a Mockingbird Harper Lee 1960 \n",
"1 2 1984 George Orwell 1949 \n",
"2 3 The Great Gatsby F. Scott Fitzgerald 1925 \n",
"3 4 The Catcher in the Rye J.D. Salinger 1951 \n",
"4 5 Pride and Prejudice Jane Austen 1813 \n",
"\n",
" created_at \n",
"0 2024-05-28 21:22:56.730528+00:00 \n",
"1 2024-05-28 21:22:56.732074+00:00 \n",
"2 2024-05-28 21:22:56.732122+00:00 \n",
"3 2024-05-28 21:22:56.732157+00:00 \n",
"4 2024-05-28 21:22:56.732190+00:00 "
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from pandas import DataFrame\n",
"\n",
"books_df = DataFrame([dict(book) for book in books])\n",
"books_df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "BuT7G929YGI7"
},
"source": [
"### Customizing Base Model Behavior"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "KHjeEFPkTrLA"
},
"source": [
"If you would like to build additional pandas functionality into your child classes, you can leverage inheritence to overwrite methods of the base model:"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "IzTC3pJfTqtQ",
"outputId": "124772ea-eda3-472f-c93d-df865bb82cf7"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"SPREADSHEET SERVICE...\n",
"DOCUMENT ID: 1Nk-UBd-3TyWZqbWSvKIIF-S_KKdrkVFRVeNrFy_F1gs\n"
]
}
],
"source": [
"from pandas import DataFrame\n",
"\n",
"class MyBaseModel(BaseModel):\n",
"\n",
" @classmethod\n",
" def records_to_df(cls):\n",
" records = cls.all()\n",
" return DataFrame([dict(record) for record in records])\n",
"\n",
"#MyBaseModel.service = service\n",
"MyBaseModel.bind(credentials=creds, document_id=GOOGLE_SHEETS_DOCUMENT_ID)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1qs4Ar_-UQd1"
},
"source": [
"Now all child models will have this capability:"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"id": "i7yBjzcWTP5Y"
},
"outputs": [],
"source": [
"class MyBook(MyBaseModel):\n",
"\n",
" SHEET_NAME = \"books\"\n",
"\n",
" COLUMNS = [\"title\", \"author\", \"year\"]"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 223
},
"id": "W82SswmAQC5R",
"outputId": "9753b1ce-5407-46ae-aa0c-787923d7a2fa"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"GET SHEET ('books')...\n"
]
},
{
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"summary": "{\n \"name\": \"books_df\",\n \"rows\": 12,\n \"fields\": [\n {\n \"column\": \"id\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 3,\n \"min\": 1,\n \"max\": 12,\n \"num_unique_values\": 12,\n \"samples\": [\n 11,\n 10,\n 1\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 12,\n \"samples\": [\n \"Harry Potter and the Philosopher's Stone\",\n \"Alice's Adventures in Wonderland\",\n \"To Kill a Mockingbird\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"author\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 11,\n \"samples\": [\n \"Virginia Woolf\",\n \"Harper Lee\",\n \"Lewis Carroll\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"year\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 56,\n \"min\": 1813,\n \"max\": 1998,\n \"num_unique_values\": 12,\n \"samples\": [\n 1997,\n 1865,\n 1960\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"created_at\",\n \"properties\": {\n \"dtype\": \"date\",\n \"min\": \"2024-05-28 21:22:56.730528+00:00\",\n \"max\": \"2024-05-28 21:22:56.734850+00:00\",\n \"num_unique_values\": 12,\n \"samples\": [\n \"2024-05-28 21:22:56.732391+00:00\",\n \"2024-05-28 21:22:56.732358+00:00\",\n \"2024-05-28 21:22:56.730528+00:00\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}",
"type": "dataframe",
"variable_name": "books_df"
},
"text/html": [
"\n",
" \n",
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" id | \n",
" title | \n",
" author | \n",
" year | \n",
" created_at | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1 | \n",
" To Kill a Mockingbird | \n",
" Harper Lee | \n",
" 1960 | \n",
" 2024-05-28 21:22:56.730528+00:00 | \n",
"
\n",
" \n",
" 1 | \n",
" 2 | \n",
" 1984 | \n",
" George Orwell | \n",
" 1949 | \n",
" 2024-05-28 21:22:56.732074+00:00 | \n",
"
\n",
" \n",
" 2 | \n",
" 3 | \n",
" The Great Gatsby | \n",
" F. Scott Fitzgerald | \n",
" 1925 | \n",
" 2024-05-28 21:22:56.732122+00:00 | \n",
"
\n",
" \n",
" 3 | \n",
" 4 | \n",
" The Catcher in the Rye | \n",
" J.D. Salinger | \n",
" 1951 | \n",
" 2024-05-28 21:22:56.732157+00:00 | \n",
"
\n",
" \n",
" 4 | \n",
" 5 | \n",
" Pride and Prejudice | \n",
" Jane Austen | \n",
" 1813 | \n",
" 2024-05-28 21:22:56.732190+00:00 | \n",
"
\n",
" \n",
"
\n",
"
\n",
"
\n",
"
\n"
],
"text/plain": [
" id title author year \\\n",
"0 1 To Kill a Mockingbird Harper Lee 1960 \n",
"1 2 1984 George Orwell 1949 \n",
"2 3 The Great Gatsby F. Scott Fitzgerald 1925 \n",
"3 4 The Catcher in the Rye J.D. Salinger 1951 \n",
"4 5 Pride and Prejudice Jane Austen 1813 \n",
"\n",
" created_at \n",
"0 2024-05-28 21:22:56.730528+00:00 \n",
"1 2024-05-28 21:22:56.732074+00:00 \n",
"2 2024-05-28 21:22:56.732122+00:00 \n",
"3 2024-05-28 21:22:56.732157+00:00 \n",
"4 2024-05-28 21:22:56.732190+00:00 "
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"books_df = MyBook.records_to_df()\n",
"books_df.head()"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}