Separating textbox strings into dropdown menu

I am trying to get the company representatives’ data from the data table and separate them individually so they can appear in the employee’s schedule section of the app. What I am struggling with is separating them into individual strings so let’s say a manager can select the employee and create a shift for them.

**What I’ve tried is the below code. That got me the data but I haven’t figured out how to make it individually. At the end of the process, I will save each employee’s email, start shift, and end shift to a new data table and display it for both employee/manager. **

**

Dropdown menu for Employee Emails

rep_list = [(str(d['Company_Representatives']),d) for d in app_tables.company_info.search()]
start =  0
end = len(rep_list)

for x in range(start, end,2):
  rep_list_individual = (rep_list[x:x+1])
  print(end)

self.drop_down_employees.items = rep_list_individual
**

Clone link:
share a copy of your app

Does this help your thinking at all?

Drop down from tables

The part of it that I got to work on is related to this topic, but unfortunately does not help me separate the text from the Company Representative column of the data table into a separate dropdown menu list. It seems like a step or two I am missing… but I cant figure out where I am going wrong.

You have a string that’s comma delimited, and want to get a list out of it. That’s a pretty typical Python question. Do a Google search on python string to list and you’ll get several results that show the technique.

Once you have a list of strings using those techniques, you can build your drop down from that.

1 Like

@jshaffstall I got the list of strings and it prints and it shows up in the dropdown menu. It just shows up as a line, instead of separate items in the dropdown menu. I cannot seem to separate it so its different selection in the dropdown menu.

I saved it in a string of lists.

It shows up as the same list, but I want it to be dropdown items. I cant figure out how to do that.

You do not have a list of strings. You have a string that contains email addresses separated by commas. You want a list of strings, but you are not there yet.

See my previous answer for how to get there.

1 Like

Try:

string = 'A,B,C,D,E'
list = string.split(",")
print(list)
['A', 'B', 'C', 'D', 'E']

I took your concept and tried to duplicate it. Got an error.

AttributeError: 'list' object has no attribute 'split'
at admin_haga_system, line 45
  called from start_form, line 21`
``
 # Dropdown menu for Employee Emails
    rep_list = [(str(d['Company_Representatives']),d) for d in app_tables.company_info.search()]
    rep_list_individual = str(rep_list)
    m = rep_list_individual.split(',')
    print(m)

So I got this code to work. I think it was a list that needed to be converted to a string and then split. I am not sure if I am correctly mentioning it but it did work so hurray!

What I am facing is that its <LiveObject: anvil.tables.Row>)’ in my dropdown list. How do I remove this and what does this Liveobject mean?

That’s the d in for d in app_tables.company_info.search().

  1. For the dropdown menu, you’re constructing a list of ordered pairs (2-tuples), as rep_list
  2. You’re converting this list-of-pairs to a string, as rep_list_individual. In the resulting sequence of characters, tuples are wrapped in parentheses, and values are separated by commas. (print it and see.) A database table row object (the d) doesn’t have a specialized conversion to string format, so it renders into text as <LiveObject: anvil.tables.Row>.
  3. You’re converting it back into a list via split. Of course, this includes both halves of each tuple.

It looks like you want just the first value (the name) from each tuple. Something like

m = [ t[0]  for t in rep_list ]

Hi @p.colbert, your bit of code got that <LiveObject: anvil.tables.Row> removed, which is excellent. However, most of the time each row in the company_info data table in the company representatives will have more than one reps. When I use m = [ t[0] for t in rep_list ] It does not split the column text for company_representative that are separated by “,”

Hint: What have you learned about split, above, that would apply here?

I believe I got it to work. The last thing that seems to be in the way of it is that it still displays some unnecessary quotations and square brackets. Any clue as to how to remove those?

Example of a printed output:
[“[‘Jrgg@gmail.com’”, " ‘Michee@gmail.com’“, " 'Tommy@gmail.com”, ’ Larry@gmail.com’, " Gerry@gmail.com’]"]

but in my dropdown menu, it shows
image

rep_list = [(str(d['Company_Representatives']),d) for d in app_tables.company_info.search()]
    rep_list_individual = str(rep_list)
    #m = rep_list_individual.split(',')
    m = [ t[0] for t in rep_list ]
    l = str(m)
    w = l.split(',')
    print(w)

When you convert composite objects into strings, you’re going to get the delimiters. It’s better to avoid producing those in the first place, and that means dealing with the subobjects directly. E.g., (not tested):

m = []
for t in rep_list:
    m.extend(t[0].split(','))

Elements of m may still have leading and/or trailing whitespace. To get rid of those, see
https://docs.python.org/3.7/library/stdtypes.html#str.strip

A short study of the Python language, its data types, and related Python functions, will serve you well. You’ll be able to answer many of your own questions that way, without having to wait for others.

Thank you. It worked. I am going to stop and go back and try to restudy lists/tuples/and for loops. Apparently I am not ready to write code.

While Python is intended to be straightforward, and Anvil is, too, when you combine the two, it adds up to a lot. No one should be expected to jump right in and do complex things right away. Give yourself plenty of time and practice. You deserve it!

Python is the foundation, so I find that’s the best place to start. Learn to see the code as Python sees it, and you’ve got a solid base for reasoning about what happens when it runs. This is critical for when things aren’t working as expected.

2 Likes

It’s really easier that you might think when you start from scratch and learn the very basic concepts first, instead of the opposite approach of ‘trying all the pegs until you find one that fits in the square hole’.

You will find that once you get just a few core concepts, python is built around the idea of using those core concepts over and over, everywhere it possibly can so most of the time, where you think you can do it a certain way, it will work out for you.

@david.wylie asked for some ‘where to start’ links from everyone in an excellent post that might interest you:

@p.colbert @ianb I went through Solo learning and Learning Python apps. My style of learning often involves “getting stuck in the pit” to figure out what happens and why is it important to do it that way.

I have learned a lot from the forums, and docs, but key basic concepts are missing. The problem with these training apps is that its too basic. Then you get stuck in something complicated because basic does work, but you want your app to work in a different way and it doesn’t. At that point your stuck hehe.

I will take a few weeks off the forums and go back to learning loops and lists which seem to be the biggest concerns. Thank you all for your help. I will be back :slight_smile:

The biggest gap I see so far has nothing to do with lists, or loops, or procedural matters of any kind. Step back from that to Python’s values, its data types, and try to see them as Python does, in expressions.

Every computation (expression) has a result of some type. Understanding which type that is, how values of that type behave, and what you can do with values of that type, those are some of the “key basic concepts” you’ve been missing.

Types are the building blocks to nearly everything else in Python, and, by extension, in Anvil. So they make a very good starting point.

Python has a very rich set of types, each with a rich set of features, so it’s easy to get lost in that maze, if you try to rush through it. Feel free to take it one digestible chunk at a time, experiment with it, see how it works.

And don’t worry about remembering every little detail. The Python on-line reference is a terrific resource. I still refer to it frequently, even after years of practice!

A separate pillar of the language would be names. Nearly everything in a Python program has a name.

For lookup purposes, names are grouped into name-spaces. This is essential. It helps keep different parts of your code from stepping on each others’ variables! That may be hard to appreciate, unless you’ve had to write big programs in languages like BASIC, which had no such protections. (Micro-managing every name in every program, to make sure that Routine #501 didn’t step on the variables that routine #1223 was using… Be glad that Python does most of that for you.)

Unfortunately, namespaces are usually created implicitly: e.g., when you import a module or package, or call a function. But we end up having to do a lot of both, so understanding when names are looked up, and which namespace(s) are consulted during lookup, can clarify a lot of things. There is a simple regularity to how Python does it. The main complication is how many

Procedural matters (loops, branches, and functions) form yet a third pillar.

1 Like