Morph has a template facility to make writing SQL and Python code easier. Two types of templates exist.

Built-in Template

When the source code is opened on the workspace, a standard template created by the Morph team can be seen in the sidebar on the right. This template acts as a snippet of the Morph framework and by copying it you can immediately start implementing the analysis part.

If you have any requests for templates, please do not hesitate to request them from shibata@morphdb.io!

Custom Template

In the templates folder, user-defined template files can be created and managed.

New files can be created from the created templates with the morph create command. You can also search for them with the morph search command.

Steps to create a template

(Step 1) Create template files

Template files are created under the templates directory.

The following placeholders can be defined during creation. (The values defined in the placeholders can be specified in the morph create command options described below, which will create the file with the replacements).

  • ${MORPH_NAME}: replaced by the template name (old alias). (Specified by the --name option of the morph create command).
  • ${MORPH_DESCRIPTION}: replaced by the template description. (Specified by the --description option of the morph create command)

E.g:

import pandas as pd

import morph
from morph import MorphGlobalContext

# morph.func is a decorator that takes in the following parameters:
# name: The identifier for the file alias. The function name will be used if not provided.
# description: The description for the function.
# output_paths: The destination paths for the output.
# output_type: The return type of the function
@morph.func(
    name="${MORPH_NAME}",
    description="${MORPH_DESCRIPTION}",
    output_paths=["_private/{name}/{now()}{ext()}"],
    output_type="dataframe",
)
def main(context: MorphGlobalContext) -> pd.DataFrame:
    return pd.DataFrame({{"key1": [3, 2, 1], "key2": [5, 4, 3]}})

(Step 2) Edit template.yaml

Edit template.yaml directly under the templates folder, see example below for Syntax.

※The src can be defined as the full path or relative to template.yaml.

E.g:

# [Description]
# This file is used to manage your local templates.
#
# [Syntax]
# This file is written in YAML format. The following is an example of the syntax.
#   E.g.)
#     templates:
#       - name: <template_name> [required]
#         title: <template_title>
#         description: <template_description>
#         src: <template_source> [required]
#         language: <template_language> [required|options:"python", "sql"]
#
# [Compiling]
# After editing this file, you need to compile it using the following command.
# This command will validate the syntax of the file and make sure your templates are available.
#   E.g.)
#     $ morph template compile

templates:
  - name: test
    title: Test Template
    description: This is a test template.
    src: ./python/test.py
    language: python

(Step 3) Compile

You can check the validity of template.yaml with the following command. Once you have created the template file and even edited template.yaml, hit the following command from the VS Code terminal.

$ morph template compile
$ # If you run it and nothing is displayed, you have succeeded.🎉

How to use templates

The templates you set up can be used in the menu on the right-hand side of the workspace or when creating a new canvas in the canvas.

Select from the list of templates

Select from canvas

How to use templates from the CLI

It can be created with the following command:

※The <template name> is the name defined in template.yaml. Or by specifying the name returned by Template API, You can also use a global template.

# morph create <File to create> -template <template name>
$ morph create src/test.py --template test