Table duplication when sorting #4703
-
First Check
Example Codedef read_logs(file_path):
rows = []
if os.path.exists(file_path):
with open(file_path, "r", encoding="utf-8") as file:
for line in file:
if (
"Od:" in line
and "Do:" in line
and "Czas:" in line
and "Zarobek:" in line
):
try:
parts = [p.strip() for p in line.strip().split("|")]
if len(parts) < 6:
print(f"Za mało pól w linii: {line}")
continue
# Data rozpoczęcia
data_rozpoczecia_raw = parts[0].replace("Od:", "").strip()
try:
data_cz, godzina_cz = data_rozpoczecia_raw.split(" ")
rok, miesiac, dzien = data_cz.split("-")
godzina, minuta, _ = godzina_cz.split(":")
data_rozpoczecia = f"{dzien}.{miesiac}.{rok} | {godzina}:{minuta}"
except Exception:
data_rozpoczecia = data_rozpoczecia_raw
# Czas
duration = parts[2].replace("Czas:", "").strip()
# Zarobek
earnings = parts[3].replace("Zarobek:", "").replace("$", "").strip()
# Serwer
serwer = parts[4].replace("Serwer:", "").strip() if "Serwer:" in parts[4] else parts[4]
# Tryb
tryb = parts[5].replace("Tryb:", "").strip() if "Tryb:" in parts[5] else parts[5]
rows.append({
"data_rozpoczecia": data_rozpoczecia,
"duration": duration,
"earnings": f"${earnings}",
"serwer": serwer,
"tryb": tryb,
})
except Exception as e:
print(f"Problem z parsowaniem linii: {line}. Błąd: {e}")
return rows
columns = [
{"name": "data_rozpoczecia", "label": "Data Rozpoczęcia", "field": "data_rozpoczecia", "sortable": True, "align": "left"},
{"name": "duration", "label": "Czas Kopania", "field": "duration", "sortable": True, "align": "left"},
{"name": "earnings", "label": "Zarobek", "field": "earnings", "sortable": True, "align": "left"},
{"name": "serwer", "label": "Serwer", "field": "serwer", "sortable": True, "align": "left"},
{"name": "tryb", "label": "Tryb", "field": "tryb", "sortable": True, "align": "left"},
]
rows_all = read_logs(log_file_path)
filtered_rows = rows_all.copy()
def update_table():
text = search_input.value.lower() if search_input.value else ""
filtered = [row for row in rows_all if any(text in str(v).lower() for v in row.values())]
filtered_rows.clear()
filtered_rows.extend(filtered)
table_with_logs.update_rows(rows=filtered_rows)
# UI Tabela
with ui.card().classes(
"backdrop-blur-md bg-transparent shadow-[0_0_15px_rgba(0,51,153,0.3)] rounded-xl text-center items-center w-full mt-4"
):
ui.label("Historia Kopania").classes("text-[#00f7ff] text-xl font-semibold mb-[-1%]")
ui.separator().classes("bg-[#00f7ff] w-[250px] h-[2px]")
search_input = ui.input(
placeholder="Szukaj...",
on_change=lambda e: update_table(),
).classes("w-[200px] absolute left-[3%] top-[4%] h-[45px]").props("outlined dense")
ui.button(
"Odśwież",
on_click=lambda: [
filtered_rows.clear(),
filtered_rows.extend(read_logs(log_file_path)),
table_with_logs.update_rows(rows=filtered_rows),
ui.notify("Odświeżono historię kopania!", type="positive", close_button="X"),
],
).classes("w-[200px] absolute right-[3%] top-[4%] h-[45px]").props("text-color='#00f7ff'")
with ui.scroll_area().classes(
"h-[200px] w-full items-center justify-center shadow-[0_0_15px_rgba(0,51,153,0.3)] rounded-xl"
).props("content-style='padding: 0px' content-active-style='padding: 0px'"):
with ui.element("div").classes("w-full h-full items-center justify-center rounded-xl"):
table_with_logs = ui.table(
columns=columns,
rows=filtered_rows,
row_key="duration",
).props("virtual-scroll flat").classes("bg-[#ffffff00]") DescriptionWhen I sort by, for example, “serwer”, when I click on it, it duplicates the whole list. How to fix it? NiceGUI Version2.16.0 Python VersionPython 3.13.3 BrowserOther Operating SystemWindows Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @Kowersky, Without access to your log file it's pretty laborious to debug. We'd first have to reverse-engineer the file format and create some dummy data. It would be much easier if you could provide a stand-alone example and also remove everything which is not necessary to reproduce the problem. The more you simplify the demo code, the higher the chances that someone is able to help. Thanks! Why is it important to provide a minimal reproducible example? |
Beta Was this translation helpful? Give feedback.
Your
row_key
is "duration", which seems to be the "Czas" column. To work as a key, it should contain unique values. But your data contains two rows with "Czas: 00:00:08" which is confusing the underlying QTable.