Skip to content

Commit

Permalink
use simple table
Browse files Browse the repository at this point in the history
  • Loading branch information
hanjinliu committed Jan 8, 2023
1 parent 1be017b commit 2693dfb
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 118 deletions.
11 changes: 0 additions & 11 deletions rst/apidoc/tabulous.widgets.rst
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
tabulous.widgets package
========================

Submodules
----------

tabulous.widgets.filtering module
---------------------------------

.. automodule:: tabulous.widgets.filtering
:members:
:undoc-members:
:show-inheritance:

Module contents
---------------

Expand Down
Binary file modified rst/fig/cell_labels.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified rst/fig/colormap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified rst/fig/colormap_interpolate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified rst/fig/column_filter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified rst/fig/dock_with_table_data_annotation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified rst/fig/edit_cell.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rst/fig/formatter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions rst/fig/generate_figs.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,36 @@ def colormap_interpolate():
viewer.resize(100, 100)
return viewer

@REG.register
def formatter():
viewer = TableViewer()
table = viewer.add_table({"length": [2.1, 3.2, 1.8, 6.3]})

@table.formatter.set("length")
def _(x: float):
return f"{x:.2f} cm"
viewer.resize(100, 100)
return viewer

@REG.register
def validator():
viewer = TableViewer()
table = viewer.add_table(
{"sample": [1, 2, 3], "volume": [0., 0., 0.]},
editable=True,
)

@table.validator.set("volume")
def _(x: float):
if x < 0:
raise ValueError("Volume must be positive.")

viewer.resize(100, 100)
table.move_iloc(1, 1)
cmds.selection.edit_current(viewer)
table.native._qtable_view._create_eval_editor("-1")
return viewer

@REG.register
def cell_labels():
viewer = TableViewer()
Expand Down
Binary file modified rst/fig/tile_tables.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added rst/fig/validator.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 20 additions & 6 deletions rst/main/columnwise_settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,24 @@ Set validator Functions
-----------------------

A validator function doesn't care about the returned value. It should raise an exception
if the input value is invalid.
if the input value is invalid. In following example, negative value is interpreted as
invalid and the editor becomes red.

.. code-block:: python
viewer = TableViewer()
viewer.add_table({"sample": [1, 2, 3], "volume": [0., 0., 0.]}, editable=True)
table = viewer.add_table(
{"sample": [1, 2, 3], "volume": [0., 0., 0.]},
editable=True,
)
@table.validator.set("volume")
def _(x: float):
if x < 0:
raise ValueError("Volume must be positive.")
.. image:: ../fig/validator.png

.. note::

A :class:`Table` object converts the input value to the data type of the column.
Expand All @@ -180,18 +186,23 @@ Set formatter function
----------------------

As usual in this chapter, you can use functions that convert a value into a string
as formatter function. The formatted strings are not necessary to satisfy the
column specific validation including data type conversion.
as formatter function.

- The formatted strings do not affect the real data.
- The formatted strings are not necessary to satisfy the column specific validation
including data type conversion.

.. code-block:: python
viewer = TableViewer()
table = viewer.open_sample("iris")
table = viewer.add_table({"length": [2.1, 3.2, 1.8, 6.3]})
@table.formatter.set("sepal_length")
@table.formatter.set("length")
def _(x: float):
return f"{x:.2f} cm"
.. image:: ../fig/formatter.png

Set formatter string
--------------------

Expand All @@ -201,6 +212,9 @@ Instead of passing a function, you can also use a ready-to-be-formatted strings.
table.formatter.set("sepal_length", "{:.2f} cm")
# or use __setitem__
table.formatter["sepal_length"] = "{:.2f} cm"
Example above is identical to passing ``"{:.2f} cm".format``.

Set Formatter in GUI
Expand Down
174 changes: 77 additions & 97 deletions rst/main/user_interface.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ User Interface
:local:
:depth: 2

.. include:: ../font.rst

Tables
======

Expand Down Expand Up @@ -37,17 +39,14 @@ People using spreadsheets usually want to name some of the cells. For instance,
calculated the mean of a column, you want to name the cell as "mean". Usually, it is done
by editing one of the adjacent cells.

+---+------+
| A | B |
+===+======+
| 1 | mean |
+---+------+
| 2 | 2.5 |
+---+------+
| 3 | |
+---+------+
| 4 | |
+---+------+
=== ====
A B
=== ====
1 mean
2 2.5
3
4
=== ====

In :mod:`tabulous`, however, you can directly name the cell using cell label. You can edit
cell labels by ``F3`` key.
Expand Down Expand Up @@ -82,124 +81,105 @@ Scalar value

If the evaluation result is a scalar value,

+---+------+--------------------------+
| | col-0| col-1|
+---+------+--------------------------+
| 0 | 10 | =np.sum(df['col-0'][0:3])|
+---+------+--------------------------+
| 1 | 20 | |
+---+------+--------------------------+
| 2 | 30 | |
+---+------+--------------------------+
==== ======= =========================
.. col-0 col-1
==== ======= =========================
0 10 :red:`=np.sum(df['col-0'][0:3])`
1 20
2 30
==== ======= =========================

it will simply update the current cell.

+---+------+------+
| | col-0| col-1|
+---+------+------+
| 0 | 10 | 60 |
+---+------+------+
| 1 | 20 | |
+---+------+------+
| 2 | 30 | |
+---+------+------+
==== ======= =====
.. col-0 col-1
==== ======= =====
0 10 60
1 20
2 30
==== ======= =====

Column vector
^^^^^^^^^^^^^

If the evaluation result is an array such as ``pd.Series``,

+---+------+-----------------------------+
| | col-0| col-1|
+---+------+-----------------------------+
| 0 | 10 | =np.cumsum(df['col-0'][0:3])|
+---+------+-----------------------------+
| 1 | 20 | |
+---+------+-----------------------------+
| 2 | 30 | |
+---+------+-----------------------------+
==== ======= ============================
.. col-0 col-1
==== ======= ============================
0 10 :red:`=np.cumsum(df['col-0'][0:3])`
1 20
2 30
==== ======= ============================

it will update the relevant cells.

+---+------+------+
| | col-0| col-1|
+---+------+------+
| 0 | 10 | 10 |
+---+------+------+
| 1 | 20 | 30 |
+---+------+------+
| 2 | 30 | 60 |
+---+------+------+
==== ======= =====
.. col-0 col-1
==== ======= =====
0 10 10
1 20 30
2 30 60
==== ======= =====

You don't have to edit the top cell. As long as the editing cell will be one of the
destinations, result will be the same.

+---+------+-----------------------------+
| | col-0| col-1|
+---+------+-----------------------------+
| 0 | 10 | |
+---+------+-----------------------------+
| 1 | 20 | =np.cumsum(df['col-0'][0:3])|
+---+------+-----------------------------+
| 2 | 30 | |
+---+------+-----------------------------+
==== ======= ============================
.. col-0 col-1
==== ======= ============================
0 10
1 20 :red:`=np.cumsum(df['col-0'][0:3])`
2 30
==== ======= ============================


Row vector
^^^^^^^^^^

An row will be updated if the result should be interpreted as a row vector.

+---+------+----------------------------------------+
| | col-0| col-1 |
+---+------+----------------------------------------+
| 0 | 10 | 20 |
+---+------+----------------------------------------+
| 1 | 20 | 40 |
+---+------+----------------------------------------+
| 2 | 30 | 60 |
+---+------+----------------------------------------+
| 3 | | =np.mean(df.loc[0:3, 'col-0':'col-1']) |
+---+------+----------------------------------------+
==== ======= ======================================
.. col-0 col-1
==== ======= ======================================
0 10 20
1 20 40
2 30 60
3 :red:`=np.mean(df.loc[0:3, 'col-0':'col-1'])`
==== ======= ======================================

will return ``pd.Series([20, 40])``, which will update the table to

+---+------+------+
| | col-0| col-1|
+---+------+------+
| 0 | 10 | 20 |
+---+------+------+
| 1 | 20 | 40 |
+---+------+------+
| 2 | 30 | 60 |
+---+------+------+
| 3 | 20 | 40 |
+---+------+------+
==== ======= =====
.. col-0 col-1
==== ======= =====
0 10 20
1 20 40
2 30 60
3 20 40
==== ======= =====


Evaluate with references
^^^^^^^^^^^^^^^^^^^^^^^^

To use cell references like Excel, use "&=" instead of "=".

+---+------+----------------------------+
| | col-0| col-1|
+---+------+----------------------------+
| 0 | 10 | &=np.mean(df['col-0'][0:3])|
+---+------+----------------------------+
| 1 | 20 | |
+---+------+----------------------------+
| 2 | 30 | |
+---+------+----------------------------+

+---+------+------+
| | col-0| col-1|
+---+------+------+
| 0 | 10 | 20 |
+---+------+------+
| 1 | 20 | |
+---+------+------+
| 2 | 30 | |
+---+------+------+
==== ======= =========================
.. col-0 col-1
==== ======= =========================
0 10 :red:`&=np.mean(df['col-0'][0:3])`
1 20
2 30
==== ======= =========================

==== ======= =====
.. col-0 col-1
==== ======= =====
0 10 20
1 20
2 30
==== ======= =====

When one of the cell is edited, the value of the destination will also be updated. For instance,
editing 10 → 40 will cause the value of ``(0, "col-1")`` to be updated to 30.
Expand Down
6 changes: 3 additions & 3 deletions tabulous/commands/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ def copy_as_markdown(viewer: TableViewerBase):


def copy_as_rst_simple(viewer: TableViewerBase):
"""Copy as rst simple table"""
"""Copy as reStructuredText (rst) simple table"""
_utils.get_table(viewer)._qwidget._copy_as_formated("rst")


def copy_as_rst_grid(viewer: TableViewerBase):
"""Copy as rst grid table"""
"""Copy as reStructuredText (rst) grid table"""
_utils.get_table(viewer)._qwidget._copy_as_formated("grid")


Expand Down Expand Up @@ -161,7 +161,7 @@ def paste_data_from_markdown(viewer: TableViewerBase):


def paste_data_from_rst(viewer: TableViewerBase):
"""Paste from reStructuredText table"""
"""Paste from reStructuredText (rst) table"""

import numpy as np
import pandas as pd
Expand Down
2 changes: 1 addition & 1 deletion tabulous/widgets/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ class Table(_DataFrameTableLayer):

_Default_Name = "table"
_qwidget: QTableLayer
native: Table
native: QTableLayer

def _create_backend(self, data: pd.DataFrame) -> QTableLayer:
from tabulous._qt import QTableLayer
Expand Down

0 comments on commit 2693dfb

Please sign in to comment.