Helper Functions
This page provides a list of all helper functions supported within RapidCanvas
Import Helper Functions
Context Management
getOrCreateContext
Purpose: Creates or retrieves a context object which is essential for most helper functions. This is typically the first function called in a notebook.
Parameters:
contextId
: String identifier for the context (used for local testing)localVars
: Local variables in the current environment (passlocals()
)entities
: Optional dictionary of local datasets with entity name as key and file path as value
Returns: A context dictionary that will be used by other helper functions
Example:
How it works:
Checks if
internalContext
already exists in local variablesFor production environments, returns an empty context
For local execution, it either:
Creates a new context with entity paths if provided
Retrieves previously prepared context data
Notes:
This function must be called at the beginning of your notebook
The context object is used by most other helper functions
For local testing, you can specify local file paths for entities
The context now includes additional fields such as:
vector_stores
: List of vector stores for embedding-based retrievalfiles_data
: List of file paths and metadataglobal_vars
: Dictionary of global variables
Parameter and Variable Management
addParam
Purpose: Adds a parameter to the context dictionary.
Parameters:
context
: The context dictionaryparamKey
: Key name for the parameterparamVal
: Value of the parameter
Example:
getParam
Purpose: Retrieves a parameter value from the context by its key.
Parameters:
context
: The context dictionaryparam
: Key name of the parameter to retrieve
Returns: The value of the parameter, or None if the parameter doesn't exist
Example:
getAllParams
Purpose: Gets a list of all parameter keys available in the context.
Parameters:
context
: The context dictionary
Returns: List of all parameter keys
Example:
get_global_var
Purpose: Retrieves a global variable from the context by key.
Parameters:
context
: The context dictionarykey
: Key name of the global variable to retrieve
Returns: The value of the global variable, or None if not found
Example:
get_secret
Purpose: Securely retrieves and decrypts secret values from the context.
Parameters:
context
: The context dictionarykey
: Key name of the secret to retrieve
Returns: The decrypted secret value
Raises: Exception if the secret is not found
Example:
How it works:
Checks if the secret exists in the context
If it's a string, decodes the base64 encoding
Decrypts the value using AES encryption with environment keys
Removes control characters from the result
If not found in context, tries to get from environment variables
Raises an exception if the secret is not found anywhere
Notes:
This function handles decryption of sensitive information
Secrets are stored encrypted in the context for security
Entity Data Management
getAllEntities
Purpose: Returns a list of all entity names available in the context.
Parameters:
context
: The context dictionary
Returns: List of entity names
Example:
getEntityData
Purpose: Loads an entity (dataset) as a pandas DataFrame.
Parameters:
context
: The context dictionaryentityName
: Name of the entity to loadinferDTypesFromSchema
: If True, will use schema to set data typesnumRows
: Optional limit on number of rows to readpandas_lib
: Optional pandas library to use (defaults to standard pandas)
Returns: DataFrame containing the entity data
Raises: Exception if the entity is not found
Example:
How it works:
Looks up the entity in the context's entity paths
Gets the schema if needed
Downloads the entity file if necessary
Reads the data into a DataFrame
Raises an informative exception if the entity doesn't exist
load_all_entities
Purpose: Loads all entities into a dictionary of DataFrames.
Parameters:
context
: The context dictionary
Returns: Dictionary with entity names as keys and DataFrames as values
Example:
getEntityFilePath
Purpose: Gets the file path for a specific entity.
Parameters:
context
: The context dictionaryentityName
: Name of the entity
Returns: File path string, or None if entity doesn't exist
Example:
get_data_from_source
Purpose: Retrieves data from various data sources.
Parameters:
source_type
: Type of data source (enum from DataSourceType)source
: Path or identifier for the data sourcename
: Optional name for the data source**options
: Additional options for the data source
Returns: Data from the source (typically a DataFrame)
Example:
write_data_to_source
Purpose: Writes data to various data destinations.
Parameters:
df
: DataFrame to writesource_type
: Type of data source (enum from DataSourceType)target
: Path or identifier for the data destinationname
: Optional name for the data source**options
: Additional options for the data source
Example:
Artifact Management
getOrCreateArtifactsDir
Purpose: Creates or retrieves a directory for storing artifacts (models, files, etc.)
Parameters:
context
: The context dictionaryartifactsId
: Identifier for the artifacts collection (auto-generated if None)purgeOld
: If True, will create a fresh directory even if it exists
Returns: Path to the artifacts directory
Example:
How it works:
If no artifactsId is provided, generates a random UUID
Adds the artifactsId to the context for tracking
Constructs the local path for the artifacts directory
Either creates a fresh directory or downloads existing artifacts
Returns the path to the artifacts directory
Notes:
Artifacts are automatically uploaded when you call
Helpers.save(context)
Use the same artifactsId to access the same artifacts in different sessions
downloadArtifacts
Purpose: Downloads artifacts from storage to the local filesystem.
Parameters:
context
: The context dictionaryartifactsId
: ID of the artifacts to download
Returns: Dictionary of file names and their paths
Example:
list_artifact_files
Purpose: Lists files in an artifact without downloading them.
Parameters:
context
: The context dictionaryartifacts_id
: ID of the artifacts to list
Returns: List of file names in the artifact
Example:
download_artifact_file
Purpose: Downloads a single file from an artifact.
Parameters:
context
: The context dictionaryartifacts_id
: ID of the artifact containing the filefile_name
: Name of the file to download
Returns: Path to the downloaded file
Example:
get_artifact
Purpose: Gets the path to a specific artifact file, downloading it if necessary.
Parameters:
context
: The context dictionaryartifact_id
: ID of the artifactartifact_name
: Name of the specific file in the artifact
Returns: Path to the artifact file
Example:
uploadArtifacts
Purpose: Uploads all artifacts in the context to remote storage.
Parameters:
context
: The context dictionary
Example:
Notes:
This is automatically called by
Helpers.save(context)
Only uploads artifacts with IDs listed in the context
Output Management
createOutputCollection
Purpose: Creates or retrieves a template output collection for storing outputs.
Parameters:
context
: The context dictionary
Returns: TemplateOutputCollection object
Example:
Notes:
Most output creation functions add to this collection automatically
This is called internally by other output functions
getOutputCollection
Purpose: Gets the existing template output collection from the context.
Parameters:
context
: The context dictionary
Returns: TemplateOutputCollection object or None if not found
Example:
createTemplateOutput
Purpose: Creates a template output of any type.
Parameters:
context
: The context dictionaryoutputName
: Name of the outputoutputType
: Type of output from OutputType enumdata
: Data for the output (typically DataFrame)dataType
: Type of data file from FileType enum (CSV, PARQUET, etc.)outputFileName
: Optional custom filenamecustom_params
: Additional parameters for the outputmetadata
: Metadata for the outputdescription
: Optional descriptiongroup
: Optional group name for organizing outputs
Returns: TemplateOutput object
Example:
Notes:
This is a general-purpose output creation function
Specialized versions exist for specific output types
Automatically handles file generation and storage
createTemplateOutputDataset
Purpose: Creates a dataset (entity) output from a DataFrame.
Parameters:
context
: The context dictionaryoutputName
: Name of the datasetdataFrame
: Pandas DataFrame with the data
Returns: TemplateOutput object
Example:
Notes:
This is a convenience wrapper around createTemplateOutput
Automatically sets outputType to ENTITY and dataType to PARQUET
create_template_output_file
Purpose: Creates a file output with the specified content.
Parameters:
context
: The context dictionaryoutput_name
: Name of the outputfile_contents
: String content of the filefile_type
: Type of file from FileType enum (TEXT, JSON, MARKDOWN, HTML). Defaults to TEXT if not specified.
Returns: TemplateOutput object
Example:
create_template_output_artifact
Purpose: Creates an artifact output for storing files.
Parameters:
context
: The context dictionaryartifact_name
: Name of the artifact (should match an artifactsId)
Returns: TemplateOutput object
Example:
Notes:
The artifact_name should match the artifactsId used with getOrCreateArtifactsDir
This registers the artifact for proper handling when the context is saved
Chart Output Functions
create_template_output_chart
Purpose: Creates a base chart output without specifying the chart type or data.
Parameters:
context
: The context dictionarytitle
: Title of the chartmetadata
: Optional metadata for the chartdescription
: Optional description of the chartgroup
: Optional group name for organizing outputs
Returns: TemplateOutput object
Example:
Notes:
This creates a basic chart output container
You'll typically use one of the more specific chart functions instead
createTemplateOutputEChart
Purpose: Creates a chart output using ECharts visualization library.
Parameters:
context
: The context dictionarychartTitle
: Title of the chartdataFrame
: Pandas DataFrame with the data to visualizechartType
: Type of chart from ChartType enum (default is TABLE)params
: Optional parameters for chart configurationdescription
: Optional description of the chartgroup
: Optional group name for organizing outputs
Returns: TemplateOutput object
Example:
Notes:
Uses ECharts library for interactive visualizations
Common chart types include BAR, LINE, PIE, SCATTER, TABLE
createTemplateOutputPlotlibChart
Purpose: Creates a chart output from a matplotlib plot.
Parameters:
context
: The context dictionarychartTitle
: Title of the chartplt
: Matplotlib pyplot object with the plotdescription
: Optional description of the chartgroup
: Optional group name for organizing outputs
Returns: TemplateOutput object
Example:
Notes:
Saves the matplotlib figure as a PNG file
The plot is automatically saved to the artifacts directory
createTemplateOutputPlotlyChart
Purpose: Creates a chart output from a Plotly figure.
Parameters:
context
: The context dictionarychartTitle
: Title of the chartplotly_fig
: Plotly figure objectdescription
: Optional description of the chartgroup
: Optional group name for organizing outputs
Returns: TemplateOutput object
Example:
Notes:
Saves the Plotly figure as an HTML file with interactive features
Uses CDN for Plotly JavaScript libraries to keep file size small
createTemplateOutputPlotlyChartAsJson
Purpose: Creates a chart output from a Plotly figure, saving it as JSON.
Parameters:
context
: The context dictionarychartTitle
: Title of the chartplotly_fig
: Plotly figure objectgroup
: Optional group name for organizing outputs
Returns: TemplateOutput object
Example:
Notes:
Saves the Plotly figure as a JSON file
This format is useful when you need to load and modify the chart later
Model and Metadata Functions
create_template_output_rc_ml_model
Purpose: Creates an output for a machine learning model.
Parameters:
context
: The context dictionarymodel_name
: Name of the modelmodel_obj
: Model object (must extend RCMLModel class)artifacts
: Dictionary of model artifactsversion
: Optional version string for the model
Returns: TemplateOutput object
Raises: Exception if model_obj doesn't extend RCMLModel
Example:
Notes:
The model class must extend RCMLModel
Artifacts should contain any files needed for the model to function
get_rc_ml_model
Purpose: Loads a previously saved ML model.
Parameters:
context
: The context dictionarymodel_name
: Name of the model to loadmodel_version
: Version of the model (default is "default")
Returns: Loaded model object
Example:
create_template_output_metadata
Purpose: Creates an output for metadata information.
Parameters:
context
: The context dictionarymetadata_list
: List of metadata objects
Returns: TemplateOutput object
Example:
get_metadata_value
Purpose: Retrieves a specific metadata value from the context.
Parameters:
context
: The context dictionarysubject_type
: Type of subject from MetadataSubjectType enumsubject_name
: Name of the subjectkey
: Metadata key to retrieve
Returns: The metadata value, or None if not found
Example:
get_all_metadata
Purpose: Retrieves all metadata from the context.
Parameters:
context
: The context dictionary
Returns: Dictionary of all metadata
Example:
create_template_output_answer
Purpose: Creates an output containing an answer or result.
Parameters:
context
: The context dictionaryanswer
: The answer text or object
Returns: TemplateOutput object
Example:
create_template_output_vector_store
Purpose: Creates an output for a vector store (used for embeddings and similarity search).
Parameters:
context
: The context dictionaryvector_store_obj
: Vector store object (must extend VectorStoreBase)
Returns: TemplateOutput object
Example:
Convenience Save Functions
These functions combine creating an output and saving it to the context in a single step.
save_output_dataset
Purpose: Creates a dataset output, adds it to the collection, and saves the context in one step.
Parameters:
context
: The context dictionaryoutput_name
: Name of the datasetdata_frame
: Pandas DataFrame with the data
Returns: The created TemplateOutput object
Example:
Notes:
Automatically adds the output to the template output collection
Calls
Helpers.save(context)
for youReturns the TemplateOutput object for reference if needed
save_output_plotly_chart_as_json
Purpose: Creates a Plotly chart output as JSON, adds it to the collection, and saves the context in one step.
Parameters:
context
: The context dictionarychart_title
: Title of the chartplotly_fig
: Plotly figure objectgroup
: Optional group name for organizing outputs
Returns: The created TemplateOutput object
Example:
save_output_plot_lib_chart
Purpose: Creates a matplotlib chart output, adds it to the collection, and saves the context in one step.
Parameters:
context
: The context dictionarychart_title
: Title of the chartplt
: Matplotlib pyplot object with the plotdescription
: Optional description of the chartgroup
: Optional group name for organizing outputs
Returns: The created TemplateOutput object
Example:
save_output_echart
Purpose: Creates an ECharts visualization, adds it to the collection, and saves the context in one step.
Parameters:
context
: The context dictionarychart_title
: Title of the chartdata_frame
: Pandas DataFrame with the data to visualizechart_type
: Type of chart from ChartType enum (default is TABLE)params
: Optional parameters for chart configurationdescription
: Optional description of the chartgroup
: Optional group name for organizing outputs
Returns: The created TemplateOutput object
Example:
save_output_chart
Purpose: Creates a base chart output, adds it to the collection, and saves the context in one step.
Parameters:
context
: The context dictionarytitle
: Title of the chartmetadata
: Optional metadata for the chartdescription
: Optional description of the chartgroup
: Optional group name for organizing outputs
Returns: The created TemplateOutput object
Example:
save_output_rc_ml_model
Purpose: Creates a machine learning model output, adds it to the collection, and saves the context in one step.
Parameters:
context
: The context dictionarymodel_name
: Name of the modelmodel_obj
: Model object (must extend RCMLModel class)artifacts
: Dictionary of model artifactsversion
: Optional version string for the model
Returns: The created TemplateOutput object
Example:
save_output_artifacts
Purpose: Creates an artifact output, adds it to the collection, and saves the context in one step.
Parameters:
context
: The context dictionaryartifact_name
: Name of the artifact
Returns: The created TemplateOutput object
Example:
save_output_answer
Purpose: Creates an answer output, adds it to the collection, and saves the context in one step.
Parameters:
context
: The context dictionaryanswer
: The answer text or object
Returns: The created TemplateOutput object
Example:
save_output_metadata
Purpose: Creates a metadata output, adds it to the collection, and saves the context in one step.
Parameters:
context
: The context dictionarymetadata_list
: List of metadata objects
Returns: The created TemplateOutput object
Example:
save_output_file
Purpose: Creates a file output, adds it to the collection, and saves the context in one step.
Parameters:
context
: The context dictionaryoutput_name
: Name of the outputfile_contents
: String content of the filefile_type
: Type of file from FileType enum (TEXT, JSON, MARKDOWN, HTML)
Returns: The created TemplateOutput object
Example:
save_output_vector_store
Purpose: Creates a vector store output, adds it to the collection, and saves the context in one step.
Parameters:
context
: The context dictionaryvector_store_obj
: Vector store object (must extend VectorStoreBase)
Returns: The created TemplateOutput object
Example:
Utility Functions
initH2o
Purpose: Initializes the H2O machine learning library.
Parameters:
h2o
: Optional H2O module (imported if None)h2oServerUrl
: URL of H2O server to connect toinit_type
: 0 for connect only, 1 (default) for init local if cannot connect
Returns: Initialized H2O module
Example:
Notes:
If no server URL is provided, tries to get from environment variable
If no server is available, creates a local instance with a random name
getChildDir
Purpose: Returns the transform directory for saving temporary files.
Parameters:
context
: The context dictionary
Returns: Path to the child directory
Example:
getTenantId
Purpose: Retrieves the tenant ID from the context.
Parameters:
context
: The context dictionary
Returns: Tenant ID string
Example:
Notes:
Checks multiple keys for backward compatibility
Returns "test-tenant" as fallback if not found
get_file_data
Purpose: Gets the content of a file by name.
Parameters:
context
: The context dictionaryfile_name
: Name of the file to read
Returns: File content as string or None if file doesn't exist
Raises: Exception if the file is not found
Example:
How it works:
Checks if
files_data
exists in the contextSearches for the file with the matching name
If the file path doesn't exist locally, downloads it
Reads and returns the file contents
Raises an exception if the file is not found, listing available files
generate_warning
Purpose: Adds a warning message to the context.
Parameters:
context
: The context dictionarywarning
: Warning message to add
Example:
Notes:
Warnings are automatically included in the output when save() is called
save
Purpose: Saves all outputs and artifacts in the context to disk.
Parameters:
context
: The context dictionary
Raises: Exception if no outputs are in the template output collection
Example:
How it works:
Checks if there are outputs in the template output collection
Only uploads artifacts if
should_materialize
is not in the context or is TrueValidates that all artifacts have corresponding outputs
Processes special output types (like ML models)
Persists all outputs to disk
Writes a configuration map with output file names and warnings
Notes:
This should be called at the end of your notebook
Handles uploading artifacts, packaging ML models, and saving all outputs
You must have at least one output in the template output collection
Uses the
should_materialize
flag to conditionally skip artifact uploading in test environments
Last updated