lightweight-charts-python icon indicating copy to clipboard operation
lightweight-charts-python copied to clipboard

[BUG] 2.1 Error when trying to delete lines

Open sebinho opened this issue 1 year ago • 1 comments

Expected Behavior

Hi,

I am writing this issue to flag an issue that used to work in 2.0.1 but does not work in 2.1. When I try to delete an existing line it returns an error. The original issue is here: https://github.com/louisnw01/lightweight-charts-python/issues/388

Current Behaviour

It is easy to reproduce by simply creating a line and then trying to remove it with following command:

for line in self.chart.lines():
            line.delete()

I get the following error.

Exception in thread Thread-2 (loop):
Traceback (most recent call last):
  File "/home/zed/repos/trading_sw/.env/lib/python3.12/site-packages/lightweight_charts/chart.py", line 89, in loop
    window.evaluate_js(arg)
  File "/home/zed/repos/trading_sw/.env/lib/python3.12/site-packages/webview/window.py", line 50, in wrapper
    return function(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/zed/repos/trading_sw/.env/lib/python3.12/site-packages/webview/window.py", line 487, in evaluate_js
    raise JavascriptException(result)
webview.errors.JavascriptException: {'name': 'NotFoundError', 'line': 6, 'column': 55, 'stack': 'removeChild@[native code]\neval code@\neval@[native code]\nglobal code@http://127.0.0.1:60360/index.html:4:33'}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.12/threading.py", line 1075, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.12/threading.py", line 1012, in run
    self._target(*self._args, **self._kwargs)
  File "/home/zed/repos/trading_sw/.env/lib/python3.12/site-packages/lightweight_charts/chart.py", line 94, in loop
    raise JavascriptException(f"\n\nscript -> '{arg}',\nerror -> {msg['name']}[{msg['line']}:{msg['column']}]\n{msg['message']}")
                                                                                                                ~~~^^^^^^^^^^^
KeyError: 'message'

See below for a reproducible example.

Reproducible Example

import pandas as pd
from lightweight_charts import Chart


def calculate_sma(df, period: int = 50):
    return pd.DataFrame({
        'time': df['date'],
        f'SMA {period}': df['close'].rolling(window=period).mean()
    }).dropna()


if __name__ == '__main__':
    chart = Chart()
    chart.legend(visible=True)

    df = pd.read_csv('/home/zed/Downloads/ohlcv.csv')
    chart.set(df)

    line = chart.create_line('SMA 50')
    sma_data = calculate_sma(df, period=50)
    line.set(sma_data)

    for line in chart.lines():
        line.delete()

    chart.show(block=True)

Environment

Any

sebinho avatar Jan 31 '25 17:01 sebinho

I had a similar issue with horizontal lines used for stop-loss. A workaround for me was to simply try/except line.delete function call.

debegr92 avatar May 14 '25 05:05 debegr92