Skip to main content

Smartsheet Python SDK and Pydantic Wrapper

Project description

smartsheet-pydantic

What is it?

Smartsheet Python SDK wrapper, incorporating Pydantic models for type validation to guarantee successful data read/updates into Smartsheet.

Smartsheet API requires the incoming data to comply with Smartsheet column types. When the types do not align then the write call can fail. In order streamline the data validation this package piggybacks off the Pydantic BaseModel structure and their data validation capabilities as a "SmartModel".

This package also provides frameworks to create a "DataSource" class which you can declaratively define your data source and attach it to the SmartModel, so you can have a standard method of querying your datasource and create/update into Smartsheet.


Getting Started

Install & Import The Module

requires python greater than 3.10 Standard installation using pip install, and package import.

pip install smartsheet-pydantic
# The package name uses underscore "_" instead of hyphen
import smartsheet_pydantic

Everything revolves around the Controller

smartsheet_pydantic package's function revolves around the Controller. Once a Controller is setup, it can facilitate data extraction from the user designated data source (RESTful API or PostgreSQL) and refresh the designated Smartsheet using an additive approach (data is updated or added, but never deleted). The Controller can also facilitate writing into the Smartsheet.

During both the write, read, or update of the Smartsheet the smartsheet_pydantic as the name suggest uses Pydantic's BaseModel to validate the data. This ensures the data has the write data type and will not fail to write to the given Smartsheet.

Defining the Controller

In order to generate the Controller we will use the SheetDetail class to collect the necessary details.

  1. sheet_id: Every Smartsheet grid has a unique id, this is used to target the Smartsheet.
  2. description: User defined description of the target Smartsheet to give some context. Can be anything.
  3. smart_model: A smartsheet-pydantic model we will create to aid in data validation (Extension of Pydantic BaseModel).
  4. source: A smartsheet-pydantic model we will create which contains the connection detail so data can be extracted from, and written into the target Smartsheet.
from smartsheet_pydantic import SheetDetail, SmartModel, DataSource


sheet_detail = SheetDetail(
    sheet_id: int = 0123456789
    description: str = "Description of the target Smartsheet"
    smart_model: SmartModel = WeatherModel # we will create this in the subsequent steps
    source: DataSource = ExamplePostgres   # we will create this in the subsequent steps
)

Defining a DataSource

There are options when creating a data source. One is a RESTful API endpoint, where the you can extend the DataSourceAPI class, and provide a url attribute. The other is a PostgreSQL database source using DataSourcePostgres, where you can provide the access details as well as the query to use, and the column name designations. We will use the latter in this example.

# DataSource class to call a RESTful API endpoint.
class WeatherRestAPI(DataSourceAPI):
    url = 'http://127.0.0.1:8000/weather_data'


# DataSource class for PostgreSQL database
class ExamplePostgres(DataSourcePostgres):
    user: str          = "username"
    password: str      = "password"
    host: str          = "host name"
    database: str      = "database name"
    query: str         = 'SELECT * FROM weather_table WHERE location = "USA"'
    columns: list[str] = [ "index", "date", "temperature", "humidity", "rain" ]

Defining a SmartModel

A SmartModel class is an extension of Pydantic BaseModel. Therefore the definition of a SmartModel is very similar. Define the data fields and their type as a class attribute.

You will also need to define 2 additional Configuration parameters.

  1. source: this is the DataSource class you defined in the previous step which is associated with this SmartModel.
  2. unique_key: is a list of column names which the model will use to define uniqueness. When data in Smartsheet is updated these columns will be used to find and update the data if the data already exists.
  3. key_mapping: if you have defined the data field names that differs from the source data, then this dictionary can be used to map and rename those columns. If the data field names are the same, the value must be set to None.
from datetime import date


class WeatherModel(SmartModel):

    index: int
    date: date
    temperature: float
    humidity: float
    rain: bool

    class Configuration
        Source = WeatherRestAPI
        unique_columns: list[int] = ['index']
        key_mapping: dict = None

Generating the controller

Now that all of the components are ready we will now take the SheetDetail instance, and provide it to the SmartsheetControllerFactory class to generate a ControllerFactory. By calling on the .get_controller() method we can generate the SmarsheetController object.

from smartsheet_pydantic import SmartsheetControllerFactory

controller_factory = SmartsheetControllerFactory()
controller = controller_factory.get_controller(sheet_detail)

Using the controller

Use the provided data source to refresh the target Smartsheet

controller.refresh_smartsheet_from_source()

Extracting data from Smartsheet

data: list[WeatherData] = controller.extract_as_smartmodel()

Manually write new data, or update existing data to Smartsheet

data: list[WeatherData]
controller.update_rows(data)

Delete all row data from Smartsheet

controller.delete_all_rows()

SmartModel

Not all data can be handled within the SmartModel. You may want to manipulate the data using Pandas or Numpy. Therefore SmartModel has methods to aid in the transforming of data into a SmartModel, as well as extracting data out into a dictionary.

Extracting Data From SmartModel

weather_data: list[WeatherData]
extracted_data: list[dict] = [smart_model.dict() for smart_model in weather_data]

print(extracted_data)

Results

[
    {
        'index': 1
        'date': date(2023, 1, 1)
        'temperature': 65.2
        'humidity': 14.5
        'rain': False
    },
    {
        'index': 2
        'date': date(2023, 1, 2)
        'temperature': 67.2
        'humidity': 14.2
        'rain': False
    },
    {
        'index': 1
        'date': date(2023, 1, 3)
        'temperature': 62.3
        'humidity': 12.2
        'rain': False
    },
]

Converting Data Into SmartModel

extracted_data: list[dict]

weather_data: list[WeatherData] = \
    [WeatherData.from_source(data) for data in extracted_data]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

smartsheet-pydantic-1.4.2.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

smartsheet_pydantic-1.4.2-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file smartsheet-pydantic-1.4.2.tar.gz.

File metadata

  • Download URL: smartsheet-pydantic-1.4.2.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for smartsheet-pydantic-1.4.2.tar.gz
Algorithm Hash digest
SHA256 800375de72b9e0434c60d8c330c5d3dff186b9d94272f0f42c73fcfd0162c86e
MD5 7a829af98335c7a728fed8f68809f403
BLAKE2b-256 203acb56b2c080130617c70fca4cf3da2f99ebab5dd041380eac656e5c12baa9

See more details on using hashes here.

File details

Details for the file smartsheet_pydantic-1.4.2-py3-none-any.whl.

File metadata

File hashes

Hashes for smartsheet_pydantic-1.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a7eb4e73b3ba609c47d385ddba26faee59f2742483c4337ba3d540247a88aef2
MD5 c499ce4de80a17e3c62c486fa5133e72
BLAKE2b-256 e537f8744df784c46faba55370496ca616d4c38364cf37d21ea0f5c5b743ce9e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page