如何在同一页面的图片中添加多个图形显示?我正在尝试将 html.Div 组件添加到以下代码中以更新页面布局以在单个页面上添加更多类似的图形,但是这些新添加的图形不会显示在页面上,只能在图片中显示旧图形。我应该修改什么元素,比如说在浏览器的 dash 应用程序的单页上添加 3 次在上传的图像中显示的图形?
import dash
import dash_core_components as dcc
import dash_html_components as html
i[enter image description here][1]mport plotly.express as px
import pandas as pd
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
# assume you have a "long-form" data frame
# see https://plotly.com/python/px-arguments/ for more options
df = pd.DataFrame({
"Fruit": ["Apples", "Oranges", "Bananas", "Apples", "Oranges", "Bananas"],
"Amount": [4, 1, 2, 2, 4, 5],
"City": ["SF", "SF", "SF", "Montreal", "Montreal", "Montreal"]
})
fig = px.bar(df, x="Fruit", y="Amount", …Run Code Online (Sandbox Code Playgroud) 我刚刚开始使用破折号。以这里为例。我想转换下面的破折号应用程序
import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
app = dash.Dash()
app.layout = html.Div([
dcc.Input(id='my-id', value='initial value', type="text"),
html.Div(id='my-div')
])
@app.callback(
Output(component_id='my-div', component_property='children'),
[Input(component_id='my-id', component_property='value')]
)
def update_output_div(input_value):
return 'You\'ve entered "{}"'.format(input_value)
if __name__ == '__main__':
app.run_server()
Run Code Online (Sandbox Code Playgroud)
在用户按下按钮而不是在输入字段的值更改时更新。我该如何实现?
我已经构建了一个可交互的仪表板,并且正在寻找一种将此应用程序导出为 HTML 格式并与他人共享的方法。
对我有什么提示吗?
我用谷歌搜索,大多数答案将我转移到以下链接。
https://plot.ly/python/getting-started-with-chart-studio/
我试图把:
import plotly.io as pio
pio.write_html(app, file='hello_world.html', auto_open=True)
Run Code Online (Sandbox Code Playgroud)
在我的 app.py 之后:
if __name__ == "__main__":
app.run_server(debug=True, port=8052)
Run Code Online (Sandbox Code Playgroud)
但它不起作用。
我们正在尝试在 plotly-dash 中生成一个实时仪表板,在生成时显示实时数据。我们通常遵循此处的指导(https://dash.plotly.com/live-updates)。
我们有一个回调,它大约每秒从源收集一大块新数据点,然后将数据附加到图形中。
当我们这样做时,对图形的更新是断断续续的,因为我们每秒都在回调上生成一个新的图形对象。我们希望图形流畅地流动,即使这意味着我们比实时数据落后一两秒。
我们正在研究动画(https://plotly.com/python/animations/),但尚不清楚我们如何将动画应用于附加到图形的实时数据流。
正如我们在"入门"的"交互"部分中所看到的,一个回调函数可以接受多个输入,但始终具有单个输出.
假设我们分别有两个块,必须在输入更改后更新.当然,最简单的方法是为每个块创建两个具有相同输入的回调.问题是请求执行两次,而一个请求足以获取所有数据.
@app.callback(
dash.dependencies.Output('element_1', 'children'),
[dash.dependencies.Input('filter', 'value')])
def callback_element_1(filter):
return get_data(filter).el1
@app.callback(
dash.dependencies.Output('element_2', 'children'),
[dash.dependencies.Input('filter', 'value')])
def callback_element_2(filter):
return get_data(filter).el2
Run Code Online (Sandbox Code Playgroud)
我找到的解决方案是将这些元素包装在单个块中,并使用单个请求完全重新呈现它.但在这种情况下,包装器中的所有静态内容也将被刷新,特别是如果初始元素在DOM中彼此远离.
@app.callback(
dash.dependencies.Output('wrapper', 'children'),
[dash.dependencies.Input('filter', 'value')])
def callback_element_wrapper(filter):
data = get_data(filter)
return html.Div(
children=[
data.el1,
# more static content
data.el2,
]
)
Run Code Online (Sandbox Code Playgroud)
那么也许有更优雅的方式可以通过一个请求输出两个或多个元素?
我想监视一些实时数据,并允许用户在与图表交互时选择自己的范围.我创建了这个小例子(从教程中得到)并且问题是,每次我更新绘图时,一切都会重置,因为update_graph_live()返回一个新的Plotly数字.(见下面的例子)
是否可以仅更新数据,因此图形不会重新加载并重置为默认视图/设置?之前我使用的是d3.js并通过websockets发送数据,因此我可以在浏览器中过滤数据.但是我想直接用Dash来做.
import dash
from dash.dependencies import Output, Event
import dash_core_components as dcc
import dash_html_components as html
from random import random
import plotly
app = dash.Dash(__name__)
app.layout = html.Div(
html.Div([
html.H4('Example'),
dcc.Graph(id='live-update-graph'),
dcc.Interval(
id='interval-component',
interval=1*1000
)
])
)
@app.callback(Output('live-update-graph', 'figure'),
events=[Event('interval-component', 'interval')])
def update_graph_live():
fig = plotly.tools.make_subplots(rows=2, cols=1, vertical_spacing=0.2)
fig['layout']['margin'] = {
'l': 30, 'r': 10, 'b': 30, 't': 10
}
fig['layout']['legend'] = {'x': 0, 'y': 1, 'xanchor': 'left'}
fig.append_trace({
'x': [1, 2, 3, 4, 5],
'y': [random() …Run Code Online (Sandbox Code Playgroud) 假设我有一个带有 20 个参数的模型,并且我为每个参数制作了一个输入组件。
[dcc.Input(type = 'number', id = 'input %i'%i) for i in range(20)]
Run Code Online (Sandbox Code Playgroud)
我想要一个按钮html.Button('populate parameters', id = 'button populate'),它应该为所有输入填充最佳预装值。
代码应如下所示,但它不起作用。
for i in range(20):
@app.callback(
dash.dependencies.Output('input %i'%i, 'value'),
[dash.dependencies.Input('button populate', 'n_clicks')]
)
def update(ignore):
return np.random.uniform()
Run Code Online (Sandbox Code Playgroud)
我是否必须为每个具有相同功能的输出编写 20 个回调?我找不到一次性制作它们的方法(循环?)
我最近开始使用 Redis 和 RQ 来运行后台进程。我构建了一个 Dash 应用程序,它在 Heroku 上运行良好,并且过去也可以在本地运行。最近,我尝试再次在本地测试同一个应用程序,但不断收到以下错误 - 尽管我使用的是 Heroku 上托管的完全相同的代码:
redis.exceptions.DataError: Invalid input of type: 'NoneType'. Convert to a byte, string or number first.
Run Code Online (Sandbox Code Playgroud)
在我的requirements.txt和Ubuntu 18.04上的虚拟环境中,我有redis v.3.0.1,rq 0.13.0
当我在终端上运行 redis-server 时,我看到使用了 Redis 4.0.9(这也让我感到困惑)。
我尝试谷歌搜索两天寻找解决方案,但没有成功。
有谁知道可能发生了什么以及如何解决这个错误?
这是完整的相关回溯:
File "/home/tom/dashenv/pb101_models/pages/cumulative_culture.py", line 1026, in stop_or_start_update
job = q.fetch_job(job_id)
File "/home/tom/dashenv/dash/lib/python3.6/site-packages/rq/queue.py", line 142, in fetch_job
self.remove(job_id)
File "/home/tom/dashenv/dash/lib/python3.6/site-packages/rq/queue.py", line 186, in remove
return self.connection.lrem(self.key, 1, job_id)
File "/home/tom/dashenv/dash/lib/python3.6/site-packages/redis/client.py", line 1580, in lrem
return self.execute_command('LREM', name, count, value)
File "/home/tom/dashenv/dash/lib/python3.6/site-packages/redis/client.py", line 754, …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 Dash 布局保存在 HTML 文件中,但找不到实现此目的的方法。奇怪的是,保存单个 Plotly 图形很容易,但不是 Dash 布局。有没有人有办法解决吗?
我看到这个问题已经在这里有了答案/sf/answers/3570951611/,但我不明白。特别是关于交互性损失的说明。可以看到在保存单个绘图时保持了交互性,因此对于整个布局应该是相同的。
以下是我已经尝试过的事情:
import dash_core_components as dcc
import dash_html_components as html
import dash
import plotly as py
import plotly.graph_objs as go
# Create two figures.
fig1 = go.Figure(data=go.Scatter(x=[0, 1, 2], y=[0, 10, 0]))
fig2 = go.Figure(data=go.Scatter(x=[0, 1, 2], y=[0, -10, 0]))
# Write fig1 to HTML. The three methods below work.
py.io.write_html(fig1, file="fig1_a.html", auto_open=True)
fig1.write_html(file="fig1_b.html", auto_open=True)
py.offline.plot(fig1, filename='fig1_c.html', auto_open=True)
# Write fig2 to HTML. The three methods below work.
py.io.write_html(fig2, …Run Code Online (Sandbox Code Playgroud) 我目前正在创建我的第一个绘图破折号应用程序,并对图形主题有疑问:
我想使用可用的plotly_dark主题,并且仅将基础颜色(背景和元素颜色)调整为自定义值。我尝试了这里提供的想法:https://plotly.com/python/templates/# saving -and-distributing-custom-themes 像这样
import plotly.graph_objects as go
import plotly.io as pio
pio.templates["plotly_dark_custom"] = go.layout.Template(
...custom definitions here...
)
pio.templates.default = "plotly_dark_custom"
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有更直观的方法,例如随后构建子主题plotly_dark
并仅覆盖颜色(提供颜色调色板并定义新的背景颜色)。
由于我对Python很陌生,所以我的知识非常有限,所以我希望你能给我一些正确方向的指导。
谢谢您,请告诉我是否应该提供有关此请求的更多详细信息。斯蒂芬