Python Code to Run Scenario in Dataiku

sj0071992
sj0071992 Partner, Dataiku DSS Core Designer, Neuron, Dataiku DSS Adv Designer, Registered, Dataiku DSS Developer, Neuron 2022, Neuron 2023 Posts: 131 Neuron

Hi Team,

Could you please let me know how we can run a Scenario using Python Script.

Thanks in Advance

Answers

  • Sergey
    Sergey Dataiker, Dataiku DSS Core Designer, Dataiku DSS & SQL, Dataiku DSS Core Concepts, Registered Posts: 365 Dataiker
    edited July 2024

    Hi @sj0071992

    In case you are looking to trigger scenarios within DSS, you can find samples on how to do this in our Python API docs:

    https://doc.dataiku.com/dss/latest/python-api/scenarios.html#run-a-scenario

    So the base one should be:

    import dataiku
    
    client = dataiku.api_client()
    project = client.get_project("<PROJECT_KEY>")
    
    scenario = project.get_scenario("<SCENARIO_ID>")
    scenario.run_and_wait()

    In case you need to do that outside of DSS, you will need to use Dataiku API client externally by passing the hostname and API key:

    https://doc.dataiku.com/dss/latest/python-api/client.html#creating-a-client-from-outside-dss

  • sj0071992
    sj0071992 Partner, Dataiku DSS Core Designer, Neuron, Dataiku DSS Adv Designer, Registered, Dataiku DSS Developer, Neuron 2022, Neuron 2023 Posts: 131 Neuron

    Hi,

    This Makes sense but here we are giving Project_Key and Scenario_Id as hardcoded values, can't we get the list of project_key and Scenario_id for that particular Node so that I can just iterate to the respective Project_Key and run the Scenarios.

    Thanks in Advance

  • Sergey
    Sergey Dataiker, Dataiku DSS Core Designer, Dataiku DSS & SQL, Dataiku DSS Core Concepts, Registered Posts: 365 Dataiker
    edited July 2024

    Hi @sj0071992

    For the project list you can use:

    list_project_keys()

    https://doc.dataiku.com/dss/latest/python-api/projects.html#basic-operations

    For the scenario list within the project:

    list_scenarios(as_type='listitems')

    https://doc.dataiku.com/dss/latest/python-api/projects.html#dataikuapi.dss.project.DSSProject.list_scenarios

    If you need to run all the scenarios for all the projects for example:

    import dataiku
    
    # initializing client
    client = dataiku.api_client()
    
    # get all project keys
    project_keys = client.list_project_keys()
    
    # iterating through all the projects keys
    for p in project_keys:
        # getting the project as the object type
        project = client.get_project(p)
    
        # iterating through all the scenarios within the project
        for scenario in project.list_scenarios(as_type="objects"):
            print("In %s project, running a scenario %s..." % (p, scenario.id))
            try: 
                scenario.run_and_wait()
                print("...finished successfully")
                print("--------------------------------------")
            except: 
                print("...finished with errors. Check the logs for more details")
                print("--------------------------------------")
    

Setup Info
    Tags
      Help me…