Error in Another Tutorial

I am following this link :

At the last step, when I says :

self.repeating_panel_1.items = anvil.server.call('get_items')

and runs, the app, it gives me error :
NameError: name ‘self’ is not defined at [Form1, line 13](javascript:void(0))

NameError: name ‘self’ is not defined at [Form1, line 13](javascript:void(0))
Error gone as I shifted the line by giving 4 spaces. But now, there is no data in the grid and no error, while in the terminal it is showing me 8 rows when I gives select * from inventory;

From your first error, it seems likely that something happened to your code as you typed it in. We won’t be able to tell what it was, until you show us the code you have now.

2 Likes

This is my code (which is nothing mine, just an example of above link) :

from ._anvil_designer import Form1Template
from anvil import *
import anvil.server

class Form1(Form1Template):

def init(self, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)

# Any code you write here will run when the form opens.

self.repeating_panel_1.items = anvil.server.call('get_items')

Day before yesterday, it was showing me a blank grid, and now without touching any part of app it is saying :

anvil.server.UplinkDisconnectedError: The uplink server for “get_items” has been disconnected at [Form1, line 13](javascript:void(0))

Its really frustrating…

The above error message is clear and direct. The uplink server program you wrote, which defines function “get_items”, is no longer responding to calls. This typically means that the program isn’t running. At the time of the call, it must be running, and must have a working connection to the Internet, in order to receive and respond to your “get_items” call.

1 Like

Let me show you my environment and code :
OS : Ubuntu 20.10

This is what I have :

(base) girish@girish-System-Product-Name:~$ python3 db.py
Connecting to wss://anvil.works/uplink
Anvil websocket open
Connected to “Default environment” as SERVER

(base) girish@girish-System-Product-Name:~$ cat db.py 
import anvil.server
import psycopg2

anvil.server.connect('xxxx')

conn = psycopg2.connect(
  host="xx.xx.xx.xx",
  user="xxxx",
  password="xxxx")
conn.set_session(autocommit=True)

cur = conn.cursor()

@anvil.server.callable
def get_items():
  cur.execute('SELECT * FROM inventory;')
  items = cur.fetchall()
  return [
    {'id': item[0], 'name': item[1], 'quantity': item[2]}
    for item in items
  ]

anvil.server.wait_forever()
(base) girish@girish-System-Product-Name:~$ psql -h 18.133.244.120 -U xxxx
Password for user brief_meager_championship: 
psql (13.4 (Ubuntu 13.4-1), server 12.1 (Debian 12.1-1.pgdg100+1))
Type "help" for help.

xxxx=> select * from inventory;
 id |   item_name    | quantity 
----+----------------+----------
  1 | Vase           |       10
  2 | Bookcase       |        5
  3 | Bathtowel      |       20
  4 | Frying Pan     |       20
  5 | Large Saucepan |       25
  6 | Small Saucepan |       25
  7 | Dinner Plate   |       15
  8 | Dining Chair   |       10
(8 rows)

xxxx=> \q
(base) girish@girish-System-Product-Name:~$ 

Now, when I runs my anvil app in my browser, it shows a blank data grid.  What is in anvil app, here it is code : 

In Form1 code is showing : 
from ._anvil_designer import Form1Template
from anvil import *
import anvil.server

class Form1(Form1Template):

  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)

    # Any code you write here will run when the form opens.

    self.repeating_panel_1.items = anvil.server.call('get_items')

In RowTemplate1 code is : 
from ._anvil_designer import RowTemplate1Template
from anvil import *
import anvil.server
class RowTemplate1(RowTemplate1Template):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)

    # Any code you write here will run when the form opens.

Kindly let me know what else I should provide you, all this is from anvil tutorial please.

A tip on formatting your code in this forum: put the following line before the start of code:
```python
and this, afterwards
```
This will reveal the indentation of your code, which is critical to knowing how Python will interpret it.

Any update please? Am I missing some important facts?

I think the status is that we are waiting on you to clean up the formatting of your posts above. (Format the python code as python code, separate from other stuff.) Another alternative (for the Anvil portion, as opposed to the uplink) would be to post a clone link to your Anvil app.

1 Like

This is clone link of my app :

https://anvil.works/build#clone:YUSYOB4JEQ4B7ROT=BJVB7GDFIAFBCMRXQF2E47QJ

And this is my db.py :

import anvil.server
import psycopg2

anvil.server.connect('SOMEKEY')

conn = psycopg2.connect(
  host="XX.XX.XX.XX",
  user="XXX",
  password="XXXXX")
conn.set_session(autocommit=True)

cur = conn.cursor()

@anvil.server.callable
def get_items():
  cur.execute('SELECT * FROM inventory;')
  items = cur.fetchall()
  return [
    {'id': item[0], 'name': item[1], 'quantity': item[2]}
    for item in items
  ]

anvil.server.wait_forever()

This is also another tutorial which I think have some mistake in the code. It is not returning any row in the data grid.

1 Like

Have you tried printing out the results of your

anvil.server.call('get_items')

The data grid you are setting it to is not set up correctly for the data you are returning.

List of dictionaries you are returning should look like:
{'id': item[0], 'name': item[1], 'quantity': item[2]}

You have set up a data grid with only two columns, looking for the keys ‘column_1’ and ‘column_2’ (the defaults).

…So set the all of the column keys to correspond to the Keys of the dictionary you are passing to it and it should work. That is if your uplink is returning a list of dictionaries like in the tutorial.

But I would start by trying to print out the data being returned.
print(anvil.server.call('get_items') )

Edit: I just looked at The tutorial and it says in one part:

2 Likes

Thanks for your reply and now it is showing the data in grid as you told to give correct Key value of sql query i.e. correct Key value as per column name, but it is showing a warning :

Warning: Form1.quantity does not exist. Trying to set the ‘refreshing_data_bindings’ event of self from Form1.

What is this warning and how to correct it, if it is required.

1 Like

I cloned your app again, it looks like you put the word quantity in some fields where it does nothing:
Under the main form:
image

…and in the data grid:
image
This made python give you the errors of:

Warning: Form1.quantity does not exist. Trying to set the 'refreshing_data_bindings' event of self from Form1.
Warning: Form1.quantity does not exist. Trying to set the 'hide' event of self.data_grid_1 from Form1.

So nothing broke and it just gave you warnings. Python actually is very helpful with errors and warning messages, everything I just wrote is actually contained in the warning messages, you just have to read them carefully.

…And I don’t mean you specifically, I just mean in everyone’s day-to-day, error messages are not very helpful and are mostly jibberish and humans that interact with software have been slowly trained to ignore all of them.

Part of the python philosophy is ‘explicit is better than implicit’, so you should really read the errors. They really do have everything you need to help yourself, and you will learn way faster fixing your problems by tracking them down yourself and fixing them.

2 Likes