如何将Python函数的计算值传递给javascript

时间:2018-08-13 16:49:53

标签: javascript python flask

我正在开发一个Webapp,它将通过HTML表单向用户提问。从字符串中提取日期,并使用JavaScript绘制日期图。字符串到日期的转换是通过Python的dateutil库完成的。

例如“我7月4日的销售额是多少”,Python函数将返回07-04-2018作为我将要使用的日期。

问题是现在将此日期作为变量传递给将进行绘图的外部JavaScript文件。有什么方法可以基于HTML表单中的字符串调用Python函数,获取日期并将其传递给JS来绘制图形吗?

此刻,我不使用JS,只是从html调用Python函数,该函数在处理日期字符串后会启动一个新网页:

<form class="has-text-centered" style="width:60%;"
action="{{url_for('intentHandler')}}" method="post"> 

Python函数QuestionHandler的定义如下:

def QuestionHandler(question):
    date = dateConverter(question)
    return render_template("graph.html", date = date)

在提交问题后,要在同一网页上生成图形,建议使用JS。因此,摆弄它之后,现在的HTML看起来像这样:

<form action="" method="" onsubmit="myFunction()" action="{{url_for('intentHandler')}}" method="post"> 
    <input type="search" placeholder="How can i help you ?">
</form>

<div class="buttonR">
    <button id="searchButton" onclick="myFunction()">Search</button>
</div>`   

外部JS文件包含以下功能:

function myFunction(){
    var x = document.getElementById("latest");
    if (x.style.display === "block") {
        x.style.display = "none";
    } else {
        x.style.display = "none";
    }

    $(loadLineGraphVizContainer);
}


function loadLineGraphVizContainer(){
    var placeholderDiv = document.getElementById("tableauIFrame");
    var url = <Graphics Library URL>;
    var options = {
        hideTabs: true,
        showVizHome: false,
        width: "950px",
        height: "430px",
        "Date": "06/01/2016", 
        "Measure Name": "Sales"
    };

    viz = new tableau.Viz(placeholderDiv, url, options);  
}

我希望由QuestionHandler函数生成JS的日期变量,然后由JS文件使用该值在同一HTML页面中绘制图形。

任何建议,我们如何完成这项任务?

0 个答案:

没有答案