Skip to content

Commit

Permalink
Merge pull request #2184 from Avaiga/docs/tabular-data-example
Browse files Browse the repository at this point in the history
Example for tabular data
  • Loading branch information
namnguyen20999 authored Nov 5, 2024
2 parents 722d15b + b26ea16 commit 437d9d0
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 1 deletion.
9 changes: 9 additions & 0 deletions doc/gui/extension/example_library/example_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ def __init__(self) -> None:
# element, exported as ExampleLabel in front-end/src/index.ts
react_component="ExampleLabel",
),
"game_table": Element(
"data",
{
"data": ElementProperty(PropertyType.data),
},
# The name of the React component (GameTable) that implements this custom
# element, exported as GameTable in front-end/src/index.ts
# react_component="GameTable",
),
}

# The implementation of the rendering for the "fraction" static element
Expand Down
80 changes: 80 additions & 0 deletions doc/gui/extension/example_library/front-end/src/GameTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import React, { useEffect, useMemo, useState } from "react";
import {
createRequestDataUpdateAction,
useDispatch,
useDispatchRequestUpdateOnFirstRender,
useModule,
TaipyDynamicProps,
TableValueType,
RowType,
RowValue,
} from "taipy-gui";

interface GameTableProps extends TaipyDynamicProps {
data: TableValueType;
}

const pageKey = "no-page-key";

const GameTable = (props: GameTableProps) => {
const { data, updateVarName = "", updateVars = "", id } = props;
const [value, setValue] = useState<Record<string, Array<RowValue>>>({});
const dispatch = useDispatch();
const module = useModule();
const refresh = data?.__taipy_refresh !== undefined;
useDispatchRequestUpdateOnFirstRender(dispatch, id, module, updateVars);

const colsOrder = useMemo(() => {
return Object.keys(value);
}, [value]);

const rows = useMemo(() => {
const rows: RowType[] = [];
if (value) {
Object.entries(value).forEach(([col, colValues]) => {
colValues.forEach((val, idx) => {
rows[idx] = rows[idx] || {};
rows[idx][col] = val;
});
});
}
return rows;
}, [value]);

useEffect(() => {
if (refresh || !data || data[pageKey] === undefined) {
dispatch(
createRequestDataUpdateAction(
updateVarName,
id,
module,
colsOrder,
pageKey,
{},
true,
"ExampleLibrary",
),
);
} else {
setValue(data[pageKey]);
}
}, [refresh, data, colsOrder, updateVarName, id, dispatch, module]);

return (
<div>
<table border={1} cellPadding={10} cellSpacing={0}>
<tbody>
{rows.map((row, index) => (
<tr key={"row" + index}>
{colsOrder.map((col, cidx) => (
<td key={"val" + index + "-" + cidx}>{row[col]}</td>
))}
</tr>
))}
</tbody>
</table>
</div>
);
};

export default GameTable;
3 changes: 2 additions & 1 deletion doc/gui/extension/example_library/front-end/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
// Note that we export the 'ColoredLabel' component as 'ExampleLabel', which is
// the name used in the element declaration in the element library.
import ColoredLabel from "./ColoredLabel";
import GameTable from "./GameTable";

export { ColoredLabel as ExampleLabel };
export { ColoredLabel as ExampleLabel, GameTable };
32 changes: 32 additions & 0 deletions doc/gui/extension/table_chess_game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2021-2024 Avaiga Private Limited
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
from example_library import ExampleLibrary

from taipy.gui import Gui

data = [
["♖", "♘", "♗", "♕", "♔", "♗", "♘", "♖"],
["♙", "♙", "♙", "♙", "♙", "♙", "♙", "♙"],
["", "", "", "", "", "", "", ""],
["", "", "", "", "", "", "", ""],
["", "", "", "", "", "", "", ""],
["", "", "", "", "", "", "", ""],
["♟", "♟", "♟", "♟", "♟", "♟", "♟", "♟"],
["♜", "♞", "♝", "♛", "♚", "♝", "♞", "♜"]
]

page = """
## Chess Game
<|{data}|example.game_table|>
"""

if __name__ == "__main__":
Gui(page, libraries=[ExampleLibrary()]).run(title="Chess Game")

0 comments on commit 437d9d0

Please sign in to comment.