louisnw01 / lightweight-charts-python

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

[BUG] Streamlit chart initialization bug #450

Open Gabsys opened 2 months ago

Gabsys commented 2 months ago

Expected Behavior

Able to load the chart successfully. It was working in v1.0.20

Current Behaviour

Error of:

UnicodeDecodeError: 'gbk' codec can't decode byte 0xa2 in position 51: illegal multibyte sequence

File "{file_path}\my_file.py", line 177, in display_intraday_chart
    chart = StreamlitChart(width=700, height=700, toolbox=True)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "{file_path}\env\Lib\site-packages\lightweight_charts\widgets.py", line 158, in __init__
    super().__init__(width, height, inner_width, inner_height, scale_candles_only, toolbox)
File "{file_path}\env\Lib\site-packages\lightweight_charts\widgets.py", line 125, in __init__
    lwc = f.read()
          ^^^^^^^^

Reproducible Example

from lightweight_charts.widgets import StreamlitChart

chart = StreamlitChart(width=700, height=700, toolbox=True)

Environment

- OS: Windows 10
- Library: 2.0.1
esteban2006 commented 2 months ago

i got the same problem @Gabsys, the reason was that I was using a private repo, it was done after I changed my ropo to public

goolulu commented 1 week ago

因为你是windows 默认字符集式gbk,读取文件他默认用gbk格式读取。

import builtins  
original_open = builtins.open  
def patched_open(*args, **kwargs):  
    kwargs.setdefault('encoding', 'utf-8')  
    return original_open(*args, **kwargs)  
builtins.open = patched_open

运行上面代码改写一下