Files
lightweight-charts-python/docs/source/tables.md
louisnw 34ce3f7199 Refactoring/Enhancements/Fixes
Breaking Changes:
- Removed the `api` parameter; callbacks no longer need to be in a specific class.
- Topbar callbacks now take a chart as an argument (see updated callback examples)
- Removed the `topbar` parameter from chart declaration. The Topbar will be automatically created upon declaration of a topbar widget.
- Removed the `searchbox` parameter from charts. It will be created upon subscribing to it in `chart.events`.
- Removed `dynamic_loading`.
- Removed ‘volume_enabled’ parameter. Volume will be enabled if the volumn column is present in the dataframe.
- Widgets’ `func` parameter is now declared last.
- Switchers take a tuple of options rather than a variable number of arguments.
- `add_hotkey` renamed to `hotkey`
- Horizontal lines now take a `func` argument rather than `interactive`. This event will emit the Line object that was moved.
- Removed the `name` parameter from `line.set`. Line object names are now declared upon creation.

Enhancements:
- Added the `button` widget to the Topbar.
- Added the color picker to the drawing context menu.
- Charts now have a `candle_data` method, which returns the current data displayed on the chart as a DataFrame.
- Fixed callbacks are now located in the `chart.events` object:
    - search (e.g `chart.events.search += on_search`)
    - new_bar
    - range_change
- Added volume to the legend
- Drawings can now be accessed through `chart.toolbox.drawings`
- added the `style` and `name` parameters to `create_line`

Bug Fixes:
- Fixed a bug causing new charts not to load after `exit` was called.
- Refactored rayline placement to ensure they do not move the visible range.
- Fixed a bug causing the visible range to shift when trendlines are moved past the final candlestick.
- Fixed a bug preventing trendlines and raylines on irregular timeframes.
- Fixed a bug causing the legend to prevent mouse input into the chart.
2023-08-14 16:06:16 +01:00

3.4 KiB

Table

width: int/float | height: int/float | headings: tuple[str] | widths: tuple[float] | alignments: tuple[str] | position: 'left'/'right'/'top'/'bottom' | draggable: bool | func: callable

Tables are panes that can be used to gain further functionality from charts. They are intended to be used for watchlists, order management, or position management. It should be accessed from the create_table common method.

The Table and Row objects act as dictionaries, and can be manipulated as such.

width/height: Either given as a percentage (a float between 0 and 1) or as an integer representing pixel size.

widths: Given as a float between 0 and 1.

position: Used as you would when creating a SubChart, representing how the table will float within the window.

draggable: If True, then the window can be dragged to any position within the window.

func: If given this will be called when a row is clicked, returning the Row object in question.


new_row (Row)

*values | id: int | -> Row

Creates a new row within the table, and returns a Row object.

if id is passed it should be unique to all other rows. Otherwise, the id will be randomly generated.

Rows can be passed a string (header) item or a tuple to set multiple headings:

row['Symbol'] = 'AAPL'
row['Symbol', 'Action'] = 'AAPL', 'BUY'

background_color

column: str | color: str

Sets the background color of the row cell.

text_color

column: str | color: str

Sets the foreground color of the row cell.

delete

Deletes the row.


clear

Clears and deletes all table rows.


format

column: str | format_str: str

Sets the format to be used for the given column. table.VALUE should be used as a placeholder for the cell value. For example:

table.format('Daily %', f'{table.VALUE} %')
table.format('PL', f'$ {table.VALUE}')

visible

visible: bool

Sets the visibility of the Table.


Tables can also have a footer containing a number of text boxes. To initialize this, call the footer attribute with the number of textboxes to be used:

table.footer(3)  # Footer will be displayed, with 3 text boxes.

To edit the textboxes, treat footer as a list:

table.footer[0] = 'Text Box 1'
table.footer[1] = 'Text Box 2'
table.footer[2] = 'Text Box 3'

Example:

import pandas as pd
from lightweight_charts import Chart

def on_row_click(row):
    row['PL'] = round(row['PL']+1, 2)
    row.background_color('PL', 'green' if row['PL'] > 0 else 'red')

    table.footer[1] = row['Ticker']

if __name__ == '__main__':
    chart = Chart(width=1000, inner_width=0.7, inner_height=1)
    subchart = chart.create_subchart(width=0.3, height=0.5)
    df = pd.read_csv('ohlcv.csv')
    chart.set(df)
    subchart.set(df)

    table = chart.create_table(width=0.3, height=0.2,
                  headings=('Ticker', 'Quantity', 'Status', '%', 'PL'),
                  widths=(0.2, 0.1, 0.2, 0.2, 0.3),
                  alignments=('center', 'center', 'right', 'right', 'right'),
                  position='left', func=on_row_click)

    table.format('PL', f{table.VALUE}')
    table.format('%', f'{table.VALUE} %')

    table.new_row('SPY', 3, 'Submitted', 0, 0)
    table.new_row('AMD', 1, 'Filled', 25.5, 105.24)
    table.new_row('NVDA', 2, 'Filled', -0.5, -8.24)

    table.footer(2)
    table.footer[0] = 'Selected:'

    chart.show(block=True)