Survey banner
Switching to Dataiku - a new area to help users who are transitioning from other tools and diving into Dataiku! CHECK IT OUT

Python Code to Run Scenario in Dataiku

sj0071992
Python Code to Run Scenario in Dataiku

Hi Team,

 

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

 

Thanks in Advance

0 Kudos
3 Replies
sergeyd
Dataiker

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

 

 

0 Kudos
sj0071992
Author

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

0 Kudos
sergeyd
Dataiker

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_s...

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

 

0 Kudos