如何将Altair图从一条Flask路线传递到另一条Flask路线?

时间:2020-11-07 01:12:55

标签: python json flask graph altair

我对Flask非常陌生。我正在开发一个Web应用程序以显示图形。我需要显示的图表之一是使用Altair。

from altair import layer

def create_altair(anomaly_id):
    .
    .
    .
    graph = (
        layer(interval, All, normals, anomalies, current_anomaly)
        .properties(width=870, height=450)
        .configure_title(fontSize=20)
    )

    return graph.to_json(), one_anomaly
@App.route("/anomaly/<int:anomaly_id>", methods=["GET", "POST"])
@login_required
def anomaly(anomaly_id):

    graph, current_anomaly = create_altair(anomaly_id)
    if """form is valide, it will be input to the database""":
        anomaly_id += 1
        return redirect(
            url_for(
                "anomaly",
                title="Anomaly Dectection",
                anomaly_id=anomaly_id,
                form=form,
                single_anomaly=current_anomaly,
                graph=graph,
            )
        )

    return render_template(
        "anomaly.html",
        title="Anomaly Dectection",
        anomaly_id=anomaly_id,
        form=form,
        single_anomaly=current_anomaly,
        graph=graph,
    )

然后,我需要将从异常路线获得的图形传递到下面的Altair图表路线。我尝试在URL链接(如本文中所示)和flask.session中使用变量。但是两者都不起作用。这些是正确的使用方法吗?还是我还没有探索其他方法?预先谢谢你!

########################
# Altair Routes
########################

@App.route("/chart/anomaly/<string:graph>")
def anomaly_chart(graph):
    return graph
<!-- Render Charts -->

<script type="text/javascript">
    function parse(url, div) {
        var opt = {
            mode: "vega-lite",
            renderer: "svg",
            actions: { export: true, source: false, editor: false }
        };

        vegaEmbed("#" + div, url, opt, function (error, result) {
            // result.view is the Vega View, url is the original Vega-Lite specification
            vegaTooltip.vegaLite(result.view, url);
        });
    }
    parse("/chart/anomaly/" + {{ graph }}, "pred");

</script>

1 个答案:

答案 0 :(得分:0)

我通过将json文件保存在本地并从另一个烧瓶路径检索此本地文件的方式解决了这个问题。