Create code scenarios via Python API.

lightnessofbein
lightnessofbein Registered Posts: 5 ✭
edited July 2024 in Using Dataiku

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()

Tagged:

Answers

  • JordanB
    JordanB Dataiker, Dataiku DSS Core Designer, Dataiku DSS Adv Designer, Registered Posts: 297 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

  • lightnessofbein
    lightnessofbein Registered Posts: 5 ✭
    edited July 2024

    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()
Setup Info
    Tags
      Help me…