Skip to content

Commit

Permalink
Enhancements & Bug Fixes
Browse files Browse the repository at this point in the history
- Line, and histogram data can now be accessed from their `data` methods.
- Fixed a bug causing `maximize` to fail if a screen is not specified
- Global events can now be coroutines or functions.
  • Loading branch information
louisnw01 committed Oct 21, 2023
1 parent 2c27ad6 commit 5bb3739
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
6 changes: 6 additions & 0 deletions lightweight_charts/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ def __init__(self, chart: 'AbstractChart', name: str = None):
self.name = name
self.num_decimals = 2
self.offset = 0
self.data = pd.DataFrame()

def _set_interval(self, df: pd.DataFrame):
if not pd.api.types.is_datetime64_any_dtype(df['time']):
Expand Down Expand Up @@ -216,20 +217,25 @@ def _single_datetime_format(self, arg):
def set(self, df: pd.DataFrame = None, format_cols: bool = True):
if df is None or df.empty:
self.run_script(f'{self.id}.series.setData([])')
self.data = pd.DataFrame()
return
if format_cols:
df = self._df_datetime_format(df, exclude_lowercase=self.name)
if self.name:
if self.name not in df:
raise NameError(f'No column named "{self.name}".')
df = df.rename(columns={self.name: 'value'})
self.data = df.copy()
self._last_bar = df.iloc[-1]
self.run_script(f'{self.id}.series.setData({js_data(df)})')

def update(self, series: pd.Series):
series = self._series_datetime_format(series, exclude_lowercase=self.name)
if self.name in series.index:
series.rename({self.name: 'value'}, inplace=True)
if series['time'] != self._last_bar['time']:
self.data.loc[self.data.index[-1]] = self._last_bar
self.data = pd.concat([self.data, series.to_frame().T], ignore_index=True)
self._last_bar = series
self.run_script(f'{self.id}.series.update({js_data(series)})')

Expand Down
6 changes: 5 additions & 1 deletion lightweight_charts/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ def __init__(self, q, start_ev, exit_ev, loaded, emit_queue, return_queue, html,
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
if screen is None:
active_screen = webview.screens[0]
width, height = active_screen.width, active_screen.height
else:
width, height = screen.width, screen.height
self.windows.append(webview.create_window(
'', html=self.html, js_api=self.callback_api,
width=width, height=height, x=x, y=y, screen=screen,
Expand Down
6 changes: 5 additions & 1 deletion lightweight_charts/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ def __iadd__(self, other):
return self

def _emit(self, *args):
self._callable(*args) if self._callable else None
if self._callable:
if asyncio.iscoroutinefunction(self._callable):
asyncio.create_task(self._callable(*args))
else:
self._callable(*args)


class JSEmitter:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

0 comments on commit 5bb3739

Please sign in to comment.