implement context manager

This commit is contained in:
louisnw
2024-05-31 16:35:21 +01:00
parent 19a4f5f114
commit ca93ddbcb1
3 changed files with 24 additions and 4 deletions

View File

@ -11,7 +11,7 @@ from .toolbox import ToolBox
from .drawings import Box, HorizontalLine, TrendLine, TwoPointDrawing, VerticalSpan
from .topbar import TopBar
from .util import (
Pane, Events, IDGen, as_enum, jbool, js_json, TIME, NUM, FLOAT,
BulkRunScript, Pane, Events, IDGen, as_enum, jbool, js_json, TIME, NUM, FLOAT,
LINE_STYLE, MARKER_POSITION, MARKER_SHAPE, CROSSHAIR_MODE,
PRICE_SCALE_MODE, marker_position, marker_shape, js_data,
)
@ -34,6 +34,7 @@ class Window:
self.script_func = script_func
self.scripts = []
self.final_scripts = []
self.bulk_run = BulkRunScript(script_func)
if run_script:
self.run_script = run_script
@ -63,7 +64,10 @@ class Window:
if self.script_func is None:
raise AttributeError("script_func has not been set")
if self.loaded:
self.script_func(script)
if self.bulk_run.enabled:
self.bulk_run.add_script(script)
else:
self.script_func(script)
elif run_last:
self.final_scripts.append(script)
else:
@ -666,8 +670,6 @@ class AbstractChart(Candlestick, Pane):
Creates and returns a Line object.
"""
self._lines.append(Line(self, name, color, style, width, price_line, price_label))
# TODO check legend still works without this
# self._lines[-1]._push_to_legend()
return self._lines[-1]
def create_histogram(

View File

@ -12,6 +12,7 @@ class Pane:
from lightweight_charts import Window
self.win: Window = window
self.run_script = window.run_script
self.bulk_run = window.bulk_run
if hasattr(self, 'id'):
return
self.id = Window._id_gen.generate()
@ -171,3 +172,20 @@ class Events:
'''),
wrapper=lambda func, c, *args: func(c, *[float(a) for a in args])
)
class BulkRunScript:
def __init__(self, script_func):
self.enabled = False
self.scripts = []
self.script_func = script_func
def __enter__(self):
self.enabled = True
def __exit__(self, *args):
self.enabled = False
self.script_func('\n'.join(self.scripts))
self.scripts = []
def add_script(self, script):
self.scripts.append(script)

View File