Skip to content

Commit

Permalink
Merge pull request #3 from fin-py/driller
Browse files Browse the repository at this point in the history
wip dash
  • Loading branch information
drillan committed Apr 19, 2022
2 parents a1c8560 + bf3687a commit da356a3
Show file tree
Hide file tree
Showing 11 changed files with 10,075 additions and 499 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
jupyter-book
nest_asyncio
pandas
pandas==1.4.2
plotly
pybotters
matplotlib
Expand Down
58 changes: 58 additions & 0 deletions source/code/dash_figure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import dash
from dash import dcc, html
import plotly.express as px

fig = px.line(x=[1, 2, 3], y=[3, 5, 2], width=400, height=400)
text = """
#### この部分はMarkdown記法で記述しています
- リスト1
- リスト2
---
```
app = dash.Dash(__name__)
app.layout = html.Div(
[
html.Div(
[html.H1("見出し1")],
id="inner1",
),
html.Div(
[
dcc.Markdown(text, style={"display": "inline-block"}),
dcc.Graph(figure=fig, style={"display": "inline-block"}),
],
id="inner2",
),
],
id="outer",
)
if __name__ == "__main__":
app.run_server(debug=True)
```
"""

app = dash.Dash(__name__)
app.layout = html.Div(
[
html.Div(
[html.H1("Markdownとグラフ")],
id="inner1",
),
html.Div(
[
dcc.Markdown(text, style={"display": "inline-block"}),
dcc.Graph(figure=fig, style={"display": "inline-block"}),
],
id="inner2",
),
],
id="outer",
)

if __name__ == "__main__":
app.run_server(debug=True)
27 changes: 27 additions & 0 deletions source/code/dash_layout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import dash
from dash import html

app = dash.Dash(__name__)

app.layout = html.Div(
[
html.Div(
[html.H1("見出し1")],
id="inner1",
),
html.Div(
[
html.H2("見出し2", style={"display": "inline-block"}),
html.Img(
src="https://www.asakura.co.jp/user_data/product_image/12258/1.jpg",
style={"display": "inline-block"},
),
],
id="inner2",
),
],
id="outer",
)

if __name__ == "__main__":
app.run_server(debug=True)
52 changes: 52 additions & 0 deletions source/code/dash_liveupdate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import dash
import pandas as pd
import plotly.graph_objs as go
from dash import dcc, html
from dash.dependencies import Input, Output

data = pd.read_pickle("../../data/btcusd_2020-07-08.pickle")
data_iter = iter(data.groupby("timestamp"))

app = dash.Dash(__name__)
app.layout = html.Div(
html.Div(
[
html.H4("BTCUSD"),
html.Div(id="live-update-text"),
dcc.Graph(id="live-graph", animate=True),
dcc.Interval(id="interval-component", interval=1000, n_intervals=0),
]
)
)


@app.callback(
Output("live-update-text", "children"),
Output("live-graph", "figure"),
Input("interval-component", "n_intervals"),
)
def update_graph_scatter(n):
t, df = next(data_iter)
timestamp = [html.P(f"timestamp: {t}")]

groupby_side = df.groupby("side")
bid = groupby_side.get_group("bid")
ask = groupby_side.get_group("ask")
fig = go.Figure()
fig.add_trace(
go.Bar(x=bid.loc[:, "size"], y=bid.loc[:, "price"], orientation="h", name="bid")
)
fig.add_trace(
go.Bar(x=ask.loc[:, "size"], y=ask.loc[:, "price"], orientation="h", name="ask")
)
fig.update_layout(
width=600,
height=800,
xaxis={"range": [0, df.loc[:, "size"].max()]},
yaxis={"range": [df.loc[:, "price"].min(), df.loc[:, "price"].max()]},
)
return timestamp, fig


if __name__ == "__main__":
app.run_server()
33 changes: 32 additions & 1 deletion source/dash.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
# Dash

時間が足りない感じなので、次回に回したほうがよさそう?
DashではHTMLのタグをPythonのオブジェクトとして構成できます
## レイアウト

```{literalinclude} ./code/dash_layout.py
:language: python
:caption: dash_layout.py
```

`dash_layout.py` を実行すると、 {numref}`dash_layout` のように描画されます

```{figure} ./resources/dash_layout.png
:name: dash_layout
Dashのレイアウト
```

## Markdown・グラフ

HTMLタグのほか、Markdown記法で書かれたテキストや、Plotlyで記述したグラフをレイアウトできます

```{literalinclude} ./code/dash_figure.py
:language: python
:caption: dash_figure.py
```

`dash_figure.py` を実行すると、 {numref}`md_figure` のように描画されます

```{figure} ./resources/md_figure.png
:name: md_figure
Markdownとグラフ
```
13 changes: 12 additions & 1 deletion source/live_update.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# リアルタイムデータの可視化

時間が足りない感じなので、次回に回したほうがよさそう?
```{literalinclude} ./code/dash_liveupdate.py
:language: python
:caption: dash_liveupdate.py
```

`dash_liveupdate.py` を実行すると、 {numref}`orderbook` のようにリアルタイムに描画されます

```{figure} ./resources/orderbook.gif
:name: orderbook
リアルタイムの板情報
```
2,488 changes: 2,355 additions & 133 deletions source/plotly.ipynb

Large diffs are not rendered by default.

7,901 changes: 7,538 additions & 363 deletions source/plotly_express.ipynb

Large diffs are not rendered by default.

Binary file added source/resources/dash_layout.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added source/resources/md_figure.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added source/resources/orderbook.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit da356a3

Please sign in to comment.