Hello, hope all is fine!
Im tryng to change color of single (entire) ligne of DataGrid from server side, any here have some possible solution to make it?
Thanks lot!
Hello, hope all is fine!
Im tryng to change color of single (entire) ligne of DataGrid from server side, any here have some possible solution to make it?
Thanks lot!
In order to populate a data grid, you provide a list of dictionaries.
You could add a 'background_color'
key to each item on the server, then use data binding to link the background color of the template form to the 'background_color'
value.
Thank you Stefano, i will try to do it right now
Hello, thanks fore replay, im trying to do it without any success, if you explain me more, on my server i call row with app_tables.table_A.add_row(**d), this work fine and add correctly all data, but difficulty is on manipulation of background row on datagrid.
Thank you!
Can you show me the code you have that collects the data to add to the data grid?
Yes of course, from my jupyter:
df = pd.DataFrame([lst],columns =[‘TIME’,‘A’,‘B’,‘C’])
print(" DT")
print(df)
if C==‘YES’:
for d in df.to_dict(orient=“records”):
app_tables.price.add_row(**d)
it add correctly to datatable and datagrid are showing well on app
This is the code that adds the rows to the table, I assume you search
the rows on the table and pass the iterator to the datagrid.
Instead of passing the row objects to the datagrid, you could convert them into dictionaries, add the column you miss and pass that to the datagrid. Something like this:
# get the desired rows
prices = app_tables.price.search(<your criteria here>
# convert them to dictionaries
prices = [dict(row) for row in prices]
# add the background_color item to each row
for row in prices:
row['background_color'] = 'red' if row['A'] > 100 else ''
In some cases, for example if the search returns thousands of rows, converting all the rows to dictionaries can be the wrong way, but this should give you an idea.
Is’t exctactly what i need thank you so much Stefano!
My way to get around this on the client side is to leave the row_iterator itself un-read by turning to dict(row) operation into a generator that only builds the dictionaries on-the-fly.
prices = (
{ **dict(row), 'background_color' : 'red' }
if row['A'] > 100 else dict(row)
for row in prices )
Hello, i try to use this logic but the datagrid row not change color, can be that i use it on:
def init(self, **properties)
This is the scipr where i make test:
from ._anvil_designer import Form3Template
from anvil import *
import anvil.server
import anvil.users
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables
class Form3(Form3Template):
def init(self, **properties):
# Set Form properties and Data Bindings.javascript:void(0)
self.init_components(**properties)
self.repeating_panel_1.items =app_tables.decision.search(tables.order_by(“TIME”,ascending=False))
prices = app_tables.decision.search(Action=“YES”)
prices = [dict(row) for row in prices]
for row in prices:
row['background_color'] = 'red' if row['A'] =='A' else 'green'
pass
In our examples we left out the part where you would edit the code in the row template of the data grid to utilize the data passed to it through the dictionary key/value of ['background_color']
.
Here is a link to the relevant docs:
Hello, thank for replay, you mean to pass this logic under this function ?
def data_row_panel_1_show(self, **event_args):
@info3 , I would start with one of the data-grid tutorials.
You could clone one of the projects in its working state and then try to use this logic to test these examples there, without having to worry if the rest of the code is working as you expect.