louisnw01 / lightweight-charts-python

Python framework for TradingView's Lightweight Charts JavaScript library.
MIT License
1.13k stars 211 forks source link

[BUG] pyside6 add layout item #213

Closed brusand closed 11 months ago

brusand commented 11 months ago

Expected Behavior

add a chart with PySide6 Layout

Current Behaviour

self.layout.addItem(self.chart)

TypeError: 'PySide6.QtWidgets.QBoxLayout.addItem' called with wrong argument types: PySide6.QtWidgets.QBoxLayout.addItem(Chart)

Reproducible Example

import sys

import ccxt
from PySide6.QtCore import QThread, Signal
from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
from lightweight_charts import Chart

class CandleFetcher(QThread):
    candle_received = Signal(dict)

    def __init__(self, exchange, symbol, interval):
        super().__init__()
        self.exchange = exchange
        self.symbol = symbol
        self.interval = interval
        self.ws = ccxt.async_support.Exchange.websocket(self.exchange, f'{symbol}@kline_{interval}')

    async def run(self):
        async with self.ws as response:
            async for data in response:
                if 'k' in data:
                    candle = data['k']
                    self.candle_received.emit(candle)

class _CandlestickChart(QWidget):
    def __init__(self):
        super().__init__()

        self.chart = Chart()
        self.layout = QVBoxLayout(self)
        self.layout.addItem(self.chart)
        #self.chart.show()

    def update_candle_chart(self, candle):
        timestamp = candle['t']
        open_price = float(candle['o'])
        high_price = float(candle['h'])
        low_price = float(candle['l'])
        close_price = float(candle['c'])
        volume = float(candle['v'])

        self.chart.add_candle(timestamp, open_price, high_price, low_price, close_price, volume)

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.candle_chart = _CandlestickChart()
        self.setCentralWidget(self.candle_chart)

        #self.candle_fetcher = CandleFetcher('binance', 'BTC/USDT', '1m')
        #self.candle_fetcher.candle_received.connect(self.candle_chart.update_candle_chart)

        #self.candle_fetcher.start()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setStyle('Fusion')
    #app.setPalette(get_darkModePalette(app))
    window = MainWindow()
    #window = FramelessWindow(titleBarClass=TitleBar)
    window.show()
    app.exec()

Environment

- OS: mac 12.1
- Library:latest
louisnw01 commented 11 months ago

Hey @brusand,

There are a few things wrong with your code:

If you're still having issues, take a look at the example in the documentation.

Louis

brusand commented 11 months ago

i create this class on my project and it works like a charm

from lightweight_charts import LWC
from PySide6.QtWebEngineWidgets import QWebEngineView
class PySide6Chart(LWC):
    def __init__(self, widget=None, volume_enabled=True):
        try:
            self.webview = QWebEngineView(widget)
        except NameError:
            raise ModuleNotFoundError('QWebEngineView was not found, and must be installed to use QtChart.')

        super().__init__(volume_enabled)

        self.webview.loadFinished.connect(self._on_js_load)
        self.webview.page().setHtml(self._html)

    def run_script(self, script): self.webview.page().runJavaScript(script)

    def _on_js_load(self):
        self.loaded = True
        for func, args, kwargs in self.js_queue:
            getattr(super(), func)(*args, **kwargs)

    def get_webview(self): return self.webview
class CandlestickChart(QWidget):
    def __init__(self):
        super().__init__()

        self.df = pd.DataFrame()
        #self.chart = Chart(c)
        self.chart = PySide6Chart(volume_enabled=False)
        #self.chart = QtChart(self)
        self.layout = QVBoxLayout(self)

        #df = pd.read_csv('result.csv')
        #self.chart.set(df)

        self.layout.addWidget(self.chart.get_webview())