Create code scenarios via Python API.

lightnessofbein
Level 2
Create code scenarios via Python API.

Hey everyone!

I was wondering, is there a way to create scenarios, similar to the following way of recipe creation? The main requirement is to be able to create code scenarios, not step-based ones.

https://doc.dataiku.com/dss/latest/python-api/flow.html

 

builder = project.new_recipe("python")

# Set the input
builder.with_input("myinputdataset")
# Create a new managed dataset for the output in the filesystem_managed connection
builder.with_new_output_dataset("grouped_dataset", "filesystem_managed")

# Set the code - builder is a PythonRecipeCreator, and has a ``with_script`` method
builder.with_script("""
import dataiku
from dataiku import recipe
input_dataset = recipe.get_inputs_as_datasets()[0]
output_dataset = recipe.get_outputs_as_datasets()[0]

df = input_dataset.get_dataframe()
df = df.groupby("something").count()
output_dataset.write_with_schema(df)
""")

recipe = builder.create()

 

 

0 Kudos
2 Replies
JordanB
Dataiker

Hi @lightnessofbein,

Yes, we have some helpful examples in the following knowledge base document: https://doc.dataiku.com/dss/latest/scenarios/custom_scenarios.html#examples

Please let us know if you have any questions.

Thanks!

Jordan

0 Kudos
lightnessofbein
Level 2
Author

Hey @JordanB!

Thanks for the answer, however, I haven't found any example of creating a scenario object similar to how it might be done with recipes in the sample I sent above. Perhaps you could still help me with that? My aim is to do smth like

#pseudo-code
builder = project.new_scenario("new_scenario")

# Set the code - builder is a PythonRecipeCreator, and has a ``with_script`` method
builder.with_script("""
def func(x, y):
    return x + y
""")

recipe = builder.create()
0 Kudos