Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flask chart added #347

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion docs/source/examples/gui_examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,26 @@ df = pd.read_csv('ohlcv.csv')
chart.set(df)

chart.load()
```
```
___

## Flask

```python
from lightweight_charts.widgets import FlaskChart
from flask import Flask
import pandas as pd

app = Flask(__name__)

@app.route('/')
def static_chart():
df = pd.read_csv('ohlcv.csv')

chart = FlaskChart(title='My Static Chart')
chart.set(df)
return chart.load()

if __name__ == '__main__':
app.run()
```
15 changes: 14 additions & 1 deletion lightweight_charts/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
from .util import parse_event_message
from lightweight_charts import abstract

from flask import render_template
import jinja2
import re

try:
import wx.html2
except ImportError:
Expand Down Expand Up @@ -121,7 +125,7 @@ def __init__(self, width=None, height=None, inner_width=1, inner_height=1,
css = f.read()
with open(abstract.INDEX.replace("test.html", 'bundle.js'), 'r') as f:
js = f.read()
with open(abstract.INDEX.replace("test.html", 'lightweight-charts.js'), 'r') as f:
with open(abstract.INDEX.replace("test.html", 'lightweight-charts.js'), 'r', encoding='utf-8') as f:
lwc = f.read()

with open(abstract.INDEX, 'r') as f:
Expand Down Expand Up @@ -152,6 +156,15 @@ def load(self):

def _load(self): pass

class FlaskChart(StaticLWC):
def __init__(self, title='Lightweight Chart', *args, **kwargs):
super().__init__(*args, **kwargs)
self.title = title

def load(self):
super().load()
self._html = re.sub(r'<title>.*?</title>', f'<title>{self.title}</title>', self._html, flags=re.DOTALL)
return render_template(jinja2.Template(f'{self._html}</script></body></html>'))

class StreamlitChart(StaticLWC):
def __init__(self, width=None, height=None, inner_width=1, inner_height=1, scale_candles_only: bool = False, toolbox: bool = False):
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
install_requires=[
'pandas',
'pywebview>=5.0.5',
'flask',
],
package_data={
'lightweight_charts': ['js/*'],
Expand Down