Datagrid: How do you guys display data to the grid?

What I’m trying to do:
I am trying to display data entry in the tables that match users email that logged in. I want to search for that users entries, and display it in data grid.

What I’ve tried and what’s not working:
I can think I am doing something wrong, but I am not getting any error message. I feel like its something super simple i am missing…

CLIENT SIDE
# Get Rows for This User
    self.repeating_panel_1.items = anvil.server.call('search_dreams')

SERVER SIDE
@anvil.server.callable
def search_dreams():
  result = [row for row in app_tables.dreams.search(userEmail=anvil.users.get_user()['email'])]
  
  return result

Clone link:
share a copy of your app

There is nothing wrong with your code, it works as expected.

The missing part is in the DataGrid column settings: you need to tell each column what data to show:


I noticed that the Main server module defines the global variables OPENAI_API_KEY and client.

If you don’t have the persistent server enabled, you are always recreating those global variables, even when you don’t need it. For example when you click on “Journal Entry”, you make a server call to get the result of a database search. That search doesn’t need those globals and yet you are wasting time creating them.

If you do have the persistent server enabled on the other hand, you need to be careful because those globals may be shared across all requests from all user sessions, and may stay there and maybe over time grow to fill up the server memory.

I just mentioned this kind of problem with globals in another post earlier today: Question on data binding with Data Grid - #9 by stefano.menci


EDIT
I also noticed inconsistent names: My_Journal, saveDreams, search_dreams, date_picker_dateOfBirth, HomeTemplate, …

Inconsistently sprinkling capital letters or underscores makes the code less readable. This may not bother you today, but as you get more familiar with Python you will realize that inconsistently named objects make the code less readable.

A little advice: please look into naming conventions with Python, or just make up your own convention and try to be more consistent. Pick one convention per object type, for example ClassNames, class_instances, TableNames, table_columns, javaScriptStuff, etc.

2 Likes

I absolutely agree with you. I need to be more consistent with my names. Ahh Its something I noticed myself, but still in the learning phase.

I taught myself python and here we are with the basics missing. Thank you for all your help :smile:

1 Like

@stefano.menci is there some documentation on presistent server enable / globals? I am new and really have no idea what they are.

This is the documentation: Anvil Docs | Server Code

Here are a few of the main concepts:

  • Every time the client contacts the server, a new instance of python is started, which loads the whole app, then the function being called is executed. This is fairly fast, but if your imports execute some code at global level (not inside a function), then they are executed every time the client contacts the server.
  • Enabling the persistent server, only available on higher level plans, allows to keep the python interpreter running. This makes the server calls more responsive, because the function is executed immediately, without spinning a new instance.
  • When the same interpreter is used, it does remember all the global variables defined in previous calls, so you need to be careful not to share data across different users
  • Sharing globals across different requests is not just a problem with leaking data to other users, it could also cause data corruption or crashes when two requests change the same global variable at the same time (look into thread safe programming)
  • Enabling persistent server does not ensure that the server is persistent. The interpreter of app A can be killed when the app B needs more resources for example.
2 Likes