Understanding Computed Vars in AG Grids

Hey Guys,

I am working on an AG Grid table (with data coming from a database) and referencing the corresponding section in the documentation. All is well, but I don’t completely understand the example provided in the docs, can you please help me with this?

From the docs:

class AGGridDatabaseState(rx.State):
    countries: list[Country]

    # Fetch data from the database using a computed variable
    @rx.var
    def data(self) -> list[dict]:
        with rx.session() as session:
            results = session.exec(select(Country)).all()
            self.countries = [
                result.dict() for result in results
            ]
        return self.countries

My understanding is that whenever the code uses/calls the data computed variable, it will always get the “latest data” available on the database. Is this correct? Is this the only reason why the author decided to use a computed var to populate self.countries (opposed to directly defining something like countries: list[Country] = self.get_countries_from_db() at the beginning of the class definition which would store a “snapshot” of the data available in the database)?

Thank you!

Yes, The assumption is correct, using the computed var retrieves all the data(countries) at its latest every time data is called in the UI.

Also you cant do the latter opposed suggestion since that would result in a syntax error:

class AGGRidDatabaseState(rx.State):
   countries: list[Country] = self.get_countries_from_db() # this will result in a syntax error
   ...