Row Objects
Classes:
|
Database Row object for Notion API |
- class Row(data=None, columns: Optional[List[str]] = None, types: Optional[List[str]] = None, title_column: Any = 0)[source]
Bases:
objectDatabase Row object for Notion API
Creating a row:
Can be created in a few different ways: 1. From a dictionary data that maps column names to values, as well as a list of types that maps column names to types .. rubric:: Example
>>> row = Row(data = {"Name": "John", "Age": 20}, types = {"Name": "title", "Age": "number"})
2. From a dictionary data that maps column names to Property Value objects .. rubric:: Example
>>> row = Row({"Name": Title("John"), "Age": Number(20)})
3. Using lists .. rubric:: Example
>>> row = Row(data = ["John", 20], columns = ["Name", "Age"], types = ["title", "number"])
An empty row can be created by passing no arguments
- param data:
A dictionary that maps column names to values, or a dictionary that maps column names to Property Value objects, or a list of values, or a dataframe, or an iterable of values. Defaults to None.
- type data:
dict|list|series|Iterable, optional
- param columns:
If data is a list, then columns is a list of column names. If data is a dictionary, then columns is a dictionary that maps column names to types. Defaults to None.
- type columns:
list|dict, optional
- param types:
A list or dict of types. Could be string or Property Schema objects. (This is useful when trying to update an existing database)
- type types:
list, optional
- param title_column:
The column name or index that contains the title of the row. Defaults to 0.
- type title_column:
str|int, optional
- raises ValueError:
If data is a dictionary, then columns must be a dictionary that maps column names to types.
- raises ValueError:
If data is a list, then columns must be a list of column names.
- raises ValueError:
If list size of data does not match the size of columns.
- returns:
A row object that can be added to a database
- rtype:
Row
Modify or add to a row:
To add or modify a row, simply set the value of a column to a new value. .. rubric:: Example
>>> row = Row(data = {"Name": "John", "Age": 20}, types = {"Name": "title", "Age": "number"}) >>> row["Name"] = "Jane" >>> row["Age"] = 25 >>> row["Is Student"] = CheckboxPropertyValue(True) >>> row["Is Student"] = True # This will automatically infer the type >>> row {'Name': 'Jane', 'Age': 25, 'Is Student': True}
To delete a column, use the del keyword or the delete method .. rubric:: Example
>>> del row["Age"] >>> row.delete("Is Student") >>> row {'Name': 'Jane'}
Methods:
from_schema(values, schema)Create a row from a schema This is useful when trying to conform to an existing database, or when customizing row creation.
delete(key)Delete a column from the row
Convert the row to a Pandas series
Attributes:
Value representation of the row using underlying representation of the property values for readability and simplicity purposes.
Returns the notion representation of the row
- classmethod from_schema(values: Dict, schema: Schema)[source]
Create a row from a schema This is useful when trying to conform to an existing database, or when customizing row creation.
All values provided will automatically be converted to the types based on the schema.
By ID is not currently supported
- Parameters:
values (dict) – A dictionary that maps column names to values
schema (Schema) – A schema object
Example
>>> schema = Schema({"Name": "title", "Age": "number"}) >>> row = Row.from_schema({"Name": "John", "Age": 20}, schema) >>> row {'Name': 'John', 'Age': 20} >>> schema = Schema.from_database(DB_ID) >>> row = Row.from_schema({"Name": "John", "Age": 20}, schema) >>> row {'Name': 'John', 'Age': 20}
- property value
Value representation of the row using underlying representation of the property values for readability and simplicity purposes.
Example
>>> row = Row({"Name": "John", "Age": 20}, {"Name": "title", "Age": "number"}) >>> row.value {"Name": "John", "Age": 20}
As opposed to row.data which returns the PropertyValue objects
- property notion
Returns the notion representation of the row