简体   繁体   English

有没有办法在后端服务器中使用 plot 和 plotly 图表,并在网络应用程序上发送交互式结果?

[英]Is there a way to plot a plotly chart in a backend server, and to send the interactive results on a webapp?

So, I'm actually making all computations in backend, generate a chart in (.png), save it to a pathfile, and communicate through AJAX the link to this newly generated image.所以,我实际上是在后端进行所有计算,在 (.png) 中生成图表,将其保存到路径文件,并通过 AJAX 与这个新生成的图像的链接进行通信。 However, such process allows me to transfer an image only.但是,这样的过程只允许我传输图像。 I'm basically converting the plot to an image.我基本上是将 plot 转换为图像。

I wonder if there is a way to transfer the entire plotly output, as an interactive chart through AJAX.想知道有没有办法把整个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.

I'd like to send to my Webapp the plotly output. I have some hints:我想将 plotly output 发送到我的 Webapp。我有一些提示:

  1. Generate the plot in my Webapp directly in JS, so the backend sends only the data from yfinance and a directive to generate it.直接在 JS 中生成我的 Webapp 中的 plot,因此后端仅发送来自 yfinance 的数据和生成它的指令。 (Quite complex, especially knowing that I have various types of plots, which are all generated in python so the Webappp is only receiving Images at this time without differentiating them). (相当复杂,特别是知道我有各种类型的图,它们都是在 python 中生成的,所以 Webappp 此时只接收图像而不区分它们)。
  2. Create an iframe directing to the plotly output ports, but not sure about this one, And also.创建一个 iframe 指向 plotly output 端口,但不确定这个,还有。 I need to save the plot results in a DB.我需要将 plot 结果保存在数据库中。

Just to clarify:只是为了澄清:

#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.
```

try fig.to_html()尝试fig.to_html()

To be honest, I do not even know what AJAX is.老实说,我什至不知道 AJAX 是什么。 So I am not using AJAX but I still manage to display the entire interactive chart on my "website".所以我没有使用 AJAX 但我仍然设法在我的“网站”上显示整个交互式图表。

Maybe this gives you some guidance and you can figure out the AJAX part for yourself.也许这会给您一些指导,您可以自己计算出 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)

Read more about include_plotlyjs here . 在此处阅读有关include_plotlyjs的更多信息。 You can put it to True and then it will directly include the javascript. It is about 3 mb I think.你可以把它设置为 True 然后它会直接包含 javascript。我认为它大约是 3 mb。 I personally use the CDN.我个人使用CDN。 So have a look at my template:所以看看我的模板:

your_template.html:你的模板.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 %}

Yeah, that works without the AJAX. Good luck trying to fiddle it in.是的,没有 AJAX 也可以。祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM