Morph allows you to perform complex data analysis using Python.
To write a Python function, simply add the @morph.func
annotation to a normal Python function and run it in Morph.
@morph.func
@morph.load_data("example_sql_cell")
def get_data_from_database(context):
sql_result_df = context.data["example_sql_cell"]
return pd.DataFrame({})
About return values
Python return values can be dataframe
, visualisation
, markdown
or json
. Create a value that matches one of these and return it as the return value.
If dataframe
is specified, it will be displayed as is as a Morph dataframe.
If visualisation
is specified, it will be displayed as is as a Morph visualisation. In this case, you can use libraries such as Plotly or Matplotlib to visualise it.
If markdown
is specified, it is displayed as is as a Morph Markdown visualization.
If json
is specified, it will be displayed as Morph’s JSON as is.
About visualisation
For visualisation, implement a function that returns a visualisation object using a library such as Plotly or Matplotlib as follows.
Here, take care not to create and return html in the function.
Plotly
import plotly.express as px
import morph
from morph import MorphGlobalContext
@morph.func(
name="${MORPH_NAME}",
description="${MORPH_DESCRIPTION}",
)
@morph.load_data("${MORPH_PARENT_NAME}")
def main(context: MorphGlobalContext) -> px.line:
data = context.data["${MORPH_PARENT_NAME}"]
# This is where you write your code.
# The `px.line` function creates a line plot using Plotly Express.
fig = px.line(data, x="X Axis", y="Y Axis", markers=True)
fig.update_layout(title="Plotly Plot")
return fig
Matplotlib
import matplotlib.pyplot as plt
import morph
from morph import MorphGlobalContext
@morph.func(
name="${MORPH_NAME}",
description="${MORPH_DESCRIPTION}",
)
@morph.load_data("${MORPH_PARENT_NAME}")
def main(context: MorphGlobalContext) -> plt.Figure:
data = context.data["${MORPH_PARENT_NAME}"]
# This is where you write your code.
# The `plot` function creates a line plot using Matplotlib.
fig, ax = plt.subplots()
ax.plot(data["X-Axis"], data["Y-Axis"], marker="o")
ax.set_title("Matplotlib Plot")
ax.set_xlabel("X-Axis")
ax.set_ylabel("Y-Axis")
return fig