pyDOMO Create users based on existing dataset

Hi,

Im trying to automate our creation of users and assign role dynamically based on our existing dataset in DOMO.

Im new to pyDOMO and currently following samples below:


user_list = pd.read_csv('UserList.csv')

def add_user_list(ul,domo_object,invite=False):

  ur = CreateUserRequest()

  users_created = []

  for index, row in ul.iterrows():

    ur.name = row['Full Name']

    ur.email = row['Email']

    ur.role = row['Role']

    users_created.append(domo_object.users.create(ur,invite))

  return(users_created)

created_users = add_user_list(user_list,domo)


It works well and however is there a way to loop the details in dataset and not in excel file?

Im currently exploring dataset.py but I dont know how to get the values for the columns, it only returns count

https://github.com/domoinc/domo-python-sdk/blob/master/examples/dataset.py

Tagged:

Best Answer

  • cedvill00
    cedvill00 Member
    edited May 2021 Answer ✓

    Thanks! @GrantSmith Got it working.

    heres my code for reference:

    #check dataset based on specified dataset_id
    retrieved_dataset = domo.datasets.get(dataset_id) 
    #export data retrieved to a string
    csv_download = datasets.data_export(retrieved_dataset['id'], include_csv_header) 
     #transform string to dataframe
    user_list = pd.read_csv(StringIO(csv_download))
    
    #loop within the user_list and read the fields and assign to DOMO objects.
    def add_user_list(ul,domo_object,invite=False):
      ur = CreateUserRequest()
      users_created = []
      for index, row in ul.iterrows():
        ur.name = row['Employee Name']
        ur.email = row['Email']
        ur.role = 'Participant'
        users_created.append(domo_object.users.create(ur,invite))
      return(users_created)
    created_users = add_user_list(user_list,domo)
    all_users = domo.users.list(500,0)
    

Answers

  • Hi @cedvill00

    You'll want to look at the ds_query method to query domo datasets:

    • ds_query - allows you to send a query to a data set, Domo will evaluate that query and sends the results back as a list or a tibble

    This will get you a result set as a dataframe to loop over like your existing method.

    Alternatively you can use ds_data_export to export an entire dataset to an in-memory string and then user pd.read_csv to convert it to a dataframe and then use your existing looping methodology. There's an example on this way in their GitHub example: https://github.com/domoinc/domo-python-sdk/blob/master/examples/dataset.py#L48

    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**
  • cedvill00
    cedvill00 Member
    edited May 2021 Answer ✓

    Thanks! @GrantSmith Got it working.

    heres my code for reference:

    #check dataset based on specified dataset_id
    retrieved_dataset = domo.datasets.get(dataset_id) 
    #export data retrieved to a string
    csv_download = datasets.data_export(retrieved_dataset['id'], include_csv_header) 
     #transform string to dataframe
    user_list = pd.read_csv(StringIO(csv_download))
    
    #loop within the user_list and read the fields and assign to DOMO objects.
    def add_user_list(ul,domo_object,invite=False):
      ur = CreateUserRequest()
      users_created = []
      for index, row in ul.iterrows():
        ur.name = row['Employee Name']
        ur.email = row['Email']
        ur.role = 'Participant'
        users_created.append(domo_object.users.create(ur,invite))
      return(users_created)
    created_users = add_user_list(user_list,domo)
    all_users = domo.users.list(500,0)
    
  • Hi @GrantSmith

    Just want to get back here instead of posting a new question.

    Maybe you know the answer.


    Im trying to create a user based on our custom made role.

    Now , CreateUserRequest() of domo only accepts role that are defaults eg Privilege, Admin, Participant.

    When I assign a different role, a role that we created, it wont accept it.

    It returns error.



  • Have you tried passing in the id of your custom role instead of the string name?

    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**
  • Jae Wilson
    Check out my 🎥 Domo Training YouTube Channel 👨‍💻

    **Say "Thanks" by clicking the ❤️ in the post that helped you.
    **Please mark the post that solves your problem by clicking on "Accept as Solution"
  • Hi @GrantSmith,

    I tried passing roleid in role already, doesn't work.

    I also checked the acceptable attr, it only accepts name, email and role, no roleid.






    Also for any custom made roles, the names are coming as blank for the existing users (manually created).

    Compared to the ones that are domo roles.









    Thanks @jaeW_at_Onyx for the reference.

    that is where I got the code :)