{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import plotly.express as px\n",
"import plotly.graph_objects as go\n",
"from jupyter_dash import JupyterDash\n",
"from dash import dcc, html\n",
"from dash.dependencies import Input, Output\n",
"\n",
"# code and plot setup\n",
"# settings\n",
"pd.options.plotting.backend = \"plotly\"\n",
"\n",
"# sample dataframe of a wide format\n",
"np.random.seed(4); cols = list('abc')\n",
"X = np.random.randn(50,len(cols)) \n",
"df=pd.DataFrame(X, columns=cols)\n",
"df.iloc[0]=0;\n",
"\n",
"# plotly figure\n",
"fig = df.plot()\n",
"\n",
"app = JupyterDash(__name__)\n",
"app.layout = html.Div([\n",
" html.H1(\"Random datastream\"),\n",
" dcc.Interval(\n",
" id='interval-component',\n",
" interval=1*1000, # in milliseconds\n",
" n_intervals=0\n",
" ),\n",
" dcc.Graph(id='graph'),\n",
"])\n",
"\n",
"# Define callback to update graph\n",
"@app.callback(\n",
" Output('graph', 'figure'),\n",
" [Input('interval-component', \"n_intervals\")]\n",
")\n",
"def streamFig(value):\n",
" \n",
" global df\n",
" \n",
" Y = np.random.randn(1,len(cols)) \n",
" df2 = pd.DataFrame(Y, columns = cols)\n",
" df = df.append(df2, ignore_index=True)#.reset_index()\n",
" df.tail()\n",
" df3=df.copy()\n",
" df3 = df3.cumsum()\n",
" fig = df3.plot()\n",
" return(fig)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"jupyter": {
"outputs_hidden": true
},
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"app.run_server(mode='inline')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}