Simple Objects and specific use case

I want to know how to assign multiple different email address to a simple objects column and then reference it when I need to check

For example I have a textarea element that I want admin to enter multiple emails and perhaps use a “,” to separate them. They are users assigned to an Admin as an example. I know how to add them to a database using the text area, but how would I access it individually?

Text area user entered tony@gmail.com, Jack@gmail.com, Marti@gmail.com right. I got it to save into a simple object column, but how do I access just a single email from that list?

Clone link:
Simple Objects

Simple object columns are powerful, because you can store Python data structures directly into them. For example, you can store a Python list, or a Python dictionary (or a Python list of dictionaries, etc) directly into the simple object column.

What you’re storing is a string, which is not the purpose of a simple object column. You should take your string of email addresses, process it and create a Python list of strings. Then put that list of strings into the simple object column.

When you later read the simple object column, you get a list of strings out of it, and can use list indexing to get to individual elements.

1 Like

Can you give me an example of how to process my example or an idea? I have created python lists before, but have not taken a list from an input like a textbox and processed it.

For non-Anvil related Python questions, a Google search is typically a good start. Try searching on create python list from string. Once you’ve used one of the examples from the search results and if you’re still having trouble, post back here with the code you’re trying.

1 Like

Thank you, it worked!

1 Like

@jshaffstall I thought I got it to work, but it broke something else hehe… So now my save_to_company_database_click(self, **event_args) does not work after I created a new button and function to update just the company reps column of my data table.

I am not getting any error message to help find the problem, so its probably something weird I am doing.

def save_to_company_database_click(self, **event_args):
    company_info = app_tables.company_info.get(Email=anvil.users.get_user()['email'])
    if company_info:
      company_info['Company_Name'] = self.company_name_textbox.text
      company_info['Admin'] = self.admin_textbox.text
      company_info['Email'] = self.email_textbox.text
      company_info['Phone_Number'] = self.phone_number_textbox.text
      company_info['Company_Representatives'] = self.company_reps_textbox.text
      alert("records have been updated")
    else:
      app_tables.company_info.add_row(Company_Name=self.company_name_textbox.text,
                                                      Admin=self.admin_textbox.text,
                                                      Email=anvil.users.get_user()['email'],
                                                      Phone_Number=self.phone_number_textbox.text,
                                                      Address=self.company_address_textbox.text, 
                                                      Company_Representatives=self.company_reps_textbox.text
                                                     )

def button_update_company_reps_click(self, **event_args):
    reps = self.company_reps_textbox.text 
    list_of_reps = reps.split(",")
    company_info_update = app_tables.company_info.get(Email=anvil.users.get_user()['email'])
    if company_info_update['Company_Representatives'] != self.company_reps_textbox.text:
      company_info_update['Company_Representatives'] = list_of_reps
      alert("Records have been updated")
    else:
      alert("Company representatives cannot be updated. Error 111")

You have a conversion

  • comma-separated string → list

for storage. I suspect you might not be doing the reverse conversion, prior to display.

What isn’t working? Does the record never get updated in the database? Does the rep info get replaced with a string?

If you can describe what’s going wrong, we can help better.

problem 1: This bit of code below stopped working. No error, just stopped working.

def save_to_company_database_click(self, **event_args):
    company_info = app_tables.company_info.get(Email=anvil.users.get_user()['email'])
    if company_info:
      company_info['Company_Name'] = self.company_name_textbox.text
      company_info['Admin'] = self.admin_textbox.text
      company_info['Email'] = self.email_textbox.text
      company_info['Phone_Number'] = self.phone_number_textbox.text
      company_info['Company_Representatives'] = self.company_reps_textbox.text
      alert("records have been updated")
    else:
      app_tables.company_info.add_row(Company_Name=self.company_name_textbox.text,
                                                      Admin=self.admin_textbox.text,
                                                      Email=anvil.users.get_user()['email'],
                                                      Phone_Number=self.phone_number_textbox.text,
                                                      Address=self.company_address_textbox.text, 
                                                      Company_Representatives=self.company_reps_textbox.text
                                                     )

Problem 2: I think I did the if/else statement incorrectly. It seems to always result in True regardless if I change the self.company_reps_textbox text.

def button_update_company_reps_click(self, **event_args):
    reps = self.company_reps_textbox.text 
    list_of_reps = reps.split(",")
    company_info_update = app_tables.company_info.get(Email=anvil.users.get_user()['email'])
    if company_info_update['Company_Representatives'] != self.company_reps_textbox.text:
      company_info_update['Company_Representatives'] = list_of_reps
      alert("Records have been updated")
    else:
      alert("Company representatives cannot be updated. Error 111")

You might be onto something. I thought I can just reference a list in the data table.

Consider that you are now writing a list into the database column. When you read it, you’ll get the same list back out. That’s probably not what the TextArea is expecting to see.

In addition to the suggestion about what happens to the list when you get it out of the data table, think about what the above line is doing versus what these lines are doing:

You’ve got a mismatch there.

Beyond those things we can observe from the code snippets, it’s hard to even know what else might be going wrong because we still don’t know how you know something is going wrong. You say the code stopped working, but don’t say how you know it isn’t working. There’s some observable behavior that isn’t what you expect (something in the UI isn’t showing right, something in the data table isn’t right, etc).

So I got this function to work somewhat properly. It does check correctly and displays the correct message.

def button_update_company_reps_click(self, **event_args):
    reps = self.company_reps_textbox.text 
    list_of_reps = reps.split(",")
    company_info_update = app_tables.company_info.get(Email=anvil.users.get_user()['email'])
    data_base_reps = list(company_info_update['Company_Representatives'])
    print(data_base_reps)
    if list_of_reps != data_base_reps:
      company_info_update['Company_Representatives'] = list_of_reps
      alert("Records have been updated")
      
    else:
      alert("Company representatives cannot be updated. Error 111")

But I cannot figure out why this function broke. No error, nothing just wont do anything.

def save_to_company_database_click(self, **event_args):
    company_info = app_tables.company_info.get(Email=anvil.users.get_user()['email'])
    if company_info:
      company_info['Company_Name'] = self.company_name_textbox.text
      company_info['Admin'] = self.admin_textbox.text
      company_info['Email'] = self.email_textbox.text
      company_info['Phone_Number'] = self.phone_number_textbox.text
      company_info['Company_Representatives'] = self.company_reps_textbox.text
      alert("records have been updated")
    else:
      app_tables.company_info.add_row(Company_Name=self.company_name_textbox.text,
                                                      Admin=self.admin_textbox.text,
                                                      Email=anvil.users.get_user()['email'],
                                                      Phone_Number=self.phone_number_textbox.text,
                                                      Address=self.company_address_textbox.text, 
                                                      Company_Representatives=self.company_reps_textbox.text
                                                     )
    #self.refresh_data_bindings()
    self.label_company_name.text = self.company_name_textbox.text
    alert("Your company data was saved.")

You are expecting to see some behavior, but instead are seeing some other behavior. It’s very hard to comment until we know what both of those behaviors are. They’re dead obvious to you, but at this distance, not to us.

My apologizes. I understand what you mean.

def save_to_company_database_click(self, **event_args):
    company_info = app_tables.company_info.get(Email=anvil.users.get_user()['email'])
    if company_info:
      company_info['Company_Name'] = self.company_name_textbox.text
      company_info['Admin'] = self.admin_textbox.text
      company_info['Email'] = self.email_textbox.text
      company_info['Phone_Number'] = self.phone_number_textbox.text
      company_info['Company_Representatives'] = self.company_reps_textbox.text
      alert("records have been updated")
    else:
      app_tables.company_info.add_row(Company_Name=self.company_name_textbox.text,
                                                      Admin=self.admin_textbox.text,
                                                      Email=anvil.users.get_user()['email'],
                                                      Phone_Number=self.phone_number_textbox.text,
                                                      Address=self.company_address_textbox.text, 
                                                      Company_Representatives=self.company_reps_textbox.text
                                                     )

The function above is supposed to update the textboxes and the data table with any changes I make in the textboxes after clicking on “SAVE”. Since the iteration of the new function

def button_update_company_reps_click(self, **event_args):

The initial function stopped working and does not produce an error. It was saving data to the data table and alerting “Records have been updated” but now nothing.

I cant figure out why it won’t save the changes made in the textboxes

Did the event handler get cleared in the button’s properties?

Also try putting a print first thing to see if the function is executed.

You are a genius! @jshaffstall. It was the event handler. :frowning_with_open_mouth:

Thanks, but I’ve just done that often enough myself to have it be something I think about now. Glad that sorted it.

1 Like