Skip to content

Commit

Permalink
Bug Fixes:
Browse files Browse the repository at this point in the history
- Screen now defaults to the active screen rather than the `0` screen.
- `null` values given in chart data are now removed and replaced with whitespace.
- Closing the chart window now cleanly exits.
- Fixed a bug causing the `offset` parameter to be greater than `0` when there is no offset.
  • Loading branch information
louisnw01 committed Sep 29, 2023
1 parent d43e7c2 commit 2bd4c7c
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/source/conf.py
Expand Up @@ -3,7 +3,7 @@
project = 'lightweight-charts-python'
copyright = '2023, louisnw'
author = 'louisnw'
release = '1.0.17.6'
release = '1.0.17.7'

extensions = [
"myst_parser",
Expand Down
4 changes: 2 additions & 2 deletions docs/source/reference/abstract_chart.md
Expand Up @@ -324,11 +324,11 @@ ___
```{py:method} hotkey(modifier: 'ctrl' | 'alt' | 'shift' | 'meta', key: 'str' | 'int' | 'tuple', func: callable)
```{py:method} hotkey(modifier: 'ctrl' | 'alt' | 'shift' | 'meta' | None, key: 'str' | 'int' | 'tuple', func: callable)
Adds a global hotkey to the chart window, which will execute the method or function given.
When using a number in `key`, it should be given as an integer. If multiple key commands are needed for the same function, a tuple can be passed to `key`.
If multiple key commands are needed for the same function, a tuple can be passed to `key`.
```
___
Expand Down
3 changes: 2 additions & 1 deletion lightweight_charts/abstract.py
Expand Up @@ -134,6 +134,7 @@ def __init__(self, chart: 'AbstractChart', name: str = None):
self._last_bar = None
self.name = name
self.num_decimals = 2
self.offset = 0

def _set_interval(self, df: pd.DataFrame):
if not pd.api.types.is_datetime64_any_dtype(df['time']):
Expand All @@ -155,7 +156,7 @@ def _set_interval(self, df: pd.DataFrame):
value = value.total_seconds()
if value == 0:
continue
elif value > self._interval:
elif value >= self._interval:
break
self.offset = value
break
Expand Down
8 changes: 5 additions & 3 deletions lightweight_charts/chart.py
Expand Up @@ -31,8 +31,8 @@ def __init__(self, q, start_ev, exit_ev, loaded, emit_queue, return_queue, html,
webview.start(debug=debug)
self.exit.set()

def create_window(self, width, height, x, y, screen=0, on_top=False, maximize=False):
screen = webview.screens[screen]
def create_window(self, width, height, x, y, screen=None, on_top=False, maximize=False):
screen = webview.screens[screen] if screen is not None else None
if maximize:
width, height = screen.width, screen.height
self.windows.append(webview.create_window(
Expand Down Expand Up @@ -69,7 +69,7 @@ class Chart(abstract.AbstractChart):
_q, _emit_q, _return_q = (mp.Queue() for _ in range(3))
_loaded_list = [mp.Event() for _ in range(MAX_WINDOWS)]

def __init__(self, width: int = 800, height: int = 600, x: int = None, y: int = None, screen: int = 0,
def __init__(self, width: int = 800, height: int = 600, x: int = None, y: int = None, screen: int = None,
on_top: bool = False, maximize: bool = False, debug: bool = False, toolbox: bool = False,
inner_width: float = 1.0, inner_height: float = 1.0, scale_candles_only: bool = False):
self._i = Chart._window_num
Expand Down Expand Up @@ -121,6 +121,7 @@ async def show_async(self, block=False):
if self._exit.is_set():
self._exit.clear()
self.is_alive = False
self.exit()
return
elif not self._emit_q.empty():
func, args = parse_event_message(self.win, self._emit_q.get())
Expand All @@ -146,4 +147,5 @@ def exit(self):
Chart._main_window_handlers = None
Chart._window_num = 0
Chart._q = mp.Queue()
Chart._exit.clear(), Chart._start.clear()
self.is_alive = False
10 changes: 8 additions & 2 deletions lightweight_charts/util.py
@@ -1,4 +1,5 @@
import asyncio
import json
from datetime import datetime
from random import choices
from typing import Literal, Union
Expand Down Expand Up @@ -34,8 +35,13 @@ def parse_event_message(window, string):


def js_data(data: Union[pd.DataFrame, pd.Series]):
orient = 'columns' if isinstance(data, pd.Series) else 'records'
return data.to_json(orient=orient, default_handler=lambda x: 'null' if pd.isna(x) else x)
if isinstance(data, pd.DataFrame):
d = data.to_dict(orient='records')
filtered_records = [{k: v for k, v in record.items() if v is not None} for record in d]
else:
d = data.to_dict()
filtered_records = {k: v for k, v in d.items()}
return json.dumps(filtered_records, indent=2)


def jbool(b: bool): return 'true' if b is True else 'false' if b is False else None
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -5,7 +5,7 @@

setup(
name='lightweight_charts',
version='1.0.17.6',
version='1.0.17.7',
packages=find_packages(),
python_requires='>=3.8',
install_requires=[
Expand Down

0 comments on commit 2bd4c7c

Please sign in to comment.