[英]Is there a way to plot a plotly chart in a backend server, and to send the interactive results on a webapp?
所以,我實際上是在后端進行所有計算,在 (.png) 中生成圖表,將其保存到路徑文件,並通過 AJAX 與這個新生成的圖像的鏈接進行通信。 但是,這樣的過程只允許我傳輸圖像。 我基本上是將 plot 轉換為圖像。
想知道有沒有辦法把整個plotly output,通過AJAX轉成一張互動圖。
import yfinance as yf
import plotly.graph_objects as go
aapl = yf.Ticker('AAPL')
ainfo = aapl.history(start=datemin, end=datemax)
#Plot the Chart
fig = go.Figure(data=go.Scatter(x=ainfo.index,y=ainfo.Close, mode='lines'),)
#DB inject plot
fig.write_image("/Users/Xlibidish/Desktop/Django/static/"+tickerZ+rx+".png")
#And then communicate the path through AJAX etc.
我想將 plotly output 發送到我的 Webapp。我有一些提示:
只是為了澄清:
#in the previous example:
fig.view()
# will be very different from
fig.write_image()
#One will be a png file, the other a pretty cool interactive chart.
```
嘗試fig.to_html()
老實說,我什至不知道 AJAX 是什么。 所以我沒有使用 AJAX 但我仍然設法在我的“網站”上顯示整個交互式圖表。
也許這會給您一些指導,您可以自己計算出 AJAX 部分。
def _give_fig():
data = # input your data here
layout = # input your layout here
fig = go.Figure(data=data, layout=layout) # build your figure with data and layout
fig.update_layout(template='simple_white') # modify your fig until ready
return fig.to_html(config={'displaylogo': False}, include_plotlyjs=False, default_width=790, default_height=600)
# returns chart as html
# displaylogo False to hide the plotly logo in chart
# default width and hight self explanatory
# include_plotlyjs --> important!
def your_view(request):
context = {"your_plot": _give_fig()}
return render(request, 'app/your_template.html', context)
在此處閱讀有關include_plotlyjs
的更多信息。 你可以把它設置為 True 然后它會直接包含 javascript。我認為它大約是 3 mb。 我個人使用CDN。 所以看看我的模板:
你的模板.html:
{% extends 'base.html' %}
{% block styles %}
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
{% endblock %}
{% block content %}
{% autoescape off %}
{{ your_plot }}
{% endautoescape %}
{% endblock %}
是的,沒有 AJAX 也可以。祝你好運。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.