Hi all, I am having difficulty updating a variable in a data table. This is just my first attempt to update values in data tables. I know I’m in the Noob stage. I appreciate any help!
When the user clicks a radio button on a client-facing page, I include this script, which should make the appropriate anvil server call:
def crls_radio_yes_clicked(self, **event_args):
anvil.server.call(‘increment_test_instance_counter’)
pass
Then in the server module I define the function for the server call, which is supposed to increment the value of count by 1:
@anvil.server.callable
def increment_test_instance_counter():
only_row = app_tables.test_instance_counter.get(row=“only”),
only_row[“count”] += 1
Which results in the following error; it appears that it sees what I’ve created as an immutable tuple. Obviously I want to change the value, so the immutability of the tuple won’t do.
TypeError: tuple indices must be integers or slices, not str at ServerModule1, line 24 called from CRLS_Consent, line 36
However, I believe my code follows the example provided in the Anvil docs discussion of updating values in rows (see https://anvil.works/docs/data-tables/data-tables-in-code
).
Thank you in advance,
Drew
Can you print the type and value of only_row[‘count’]? I assume you expect the value to be a number but it would be nice to confirm that.
Also, as an aside, you can format your code nicely on the forum by wrapping it in backticks with the work python. Example:
```python
print('this will be syntax highlighted')
```
It seems I’m unable to do that. Here is the code I used to try to get that info:
@anvil.server.callable
def increment_test_instance_counter():
only_row = app_tables.test_instance_counter.get(row="only"),
print(type(only_row["count"])),
print(only_row["count"])
And here is the resulting error message:
TypeError: tuple indices must be integers or slices, not str at [ServerModule1, line 24](javascript:void(0)) called from [CRLS_Consent, line 36](javascript:void(0))
Thanks!
Drew
What line is the error on?
What type is the column “row”? (you can see it in the datatables service)
I’m thinking that “row” is actually a linked row to another datatable (hence the tuple reference).
Error is on this line (for the current effort to determine type):
print(type(only_row["count"])),
Column “row” is type text.
I don’t think “row” is linked to another data table. However, I did use name “row” in two other data tables. But I didn’t link either to this data table. This data table is really just going to be a placeholder for a most recently assigned ID number for another table.
Best,
Drew
Is it possible to share a clone (click
and follow instructions)? If you are not comfortable sharing a clone, please do not.
I tried clicking the gear icon but no instructions appeared.
Please read cloning instructions here:
https://anvil.works/docs/editor/cloning
Hmm, I could not clone. I got “app clone failed”. Something about your app (or the clone link you shared) seems broken/corrupted in some way. Maybe this relates to your issue.
Perhaps you could start afresh with a new app. Create a datatable with a number column, make a server call to increment it. If that doesn’t work, please try sharing a clone again.
Yikes. That would be distressing…Okay, will do. First things first.
You can also go back to an earlier version by clicking the
icon in the IDE and following the instructions. I’ve never had a corrupted app ever, so I think we can resolve this without too much stress.
Okay, I tried to replicate the problem by creating an app with only a radio button, a small table, and the server module. I created a new error. It suggests there’s a problem in token creation.
SyntaxError: bad token T_OP
at main, line 20
Here’s the clone of the new app.
https://anvil.works/build#clone:PCIXQRUAJLZLUBJY=APSYCTJVKRHBCUMQPWMFNTPL
Thanks,
Drew
Ah ha.
Those quotations are not valid characters.

Try pasting these lines in instead (I’m not sure where your quotations are coming from).
anvil.server.call('increment_test_instance_counter')
only_row = app_tables.test_instance_counter.get(row='only'),
only_row['count'] += 1
You can see that the editor is underlining things in red, meaning that there is a syntax issue. Anyway, replacing with valid quote characters seems to get us back to the original error.
Solved.
You had a comma after this line, making a tuple expression accidently:
only_row = app_tables.test_instance_counter.get(row="only"),
only_row["count"] += 1
Remove the trailing comma on the first of those lines and problem solved.
2 Likes
Okay…not sure what happened either with the invalid quotations. But at least I am aware now I can make that error. 
Thanks for catching my comma error. Will avoid that in future too.
Have a great weekend!
1 Like
Okay, with the changes–coupled with getting rid of underscores in the name of the Data Tables–it now works in the test app. I’ll try to implement the fixes in the full app. Thanks for helping and talking me off the ledge!
2 Likes