python AJAX FLASK GET刷新


                    

这是我的第一篇文章,我总是找到有用的信息,但是这次轮到我干了! </ p>

我正在尝试显示和刷新html上的数据出价和要价,数据是在python模块中生成的。 我正在尝试使用jQuery和Ajax。</ p>

这里是我要发送的JSON,更准确地说,我打算发送包含每对货币的列表,这里是 USDJPY </ code>的单个条目。</ p>

  {'Bid':109.355,'Ask':109.368}
</ code> </ pre>

这是python代码:</ p>

  import oandapy
从烧瓶导入Flask,render_template,request,jsonify

app = Flask(__ name__)

def getspot():
     oanda = oandapy.API(environment =“ practice”,access_token =“ XXXXX”)

     响应= oanda.get_prices(instruments =“ USD_JPY”)

     价格= response.get(“价格”)

     #posts = {“出价”:biding_price,“询问”:asking_price}
     posts = [dict(Bid = prices [0] .get(“ bid”),Ask = prices [0] .get(“ ask”))]

     返回帖子

@ app.route('/ _ update_USDJPY',methods = [“ GET”])
def getUSDJY():

     返回jsonify(getspot())

@ app.route('/',methods = [“ GET”])
def index():

     帖子= getspot()

     返回render_template(“ basic2.html”)

如果__name __ ==“ __ main__”:
     app.run()
</ code> </ pre>

这是我的html页面:</ p>

   &lt;/ script&gt;</p>

<p><script type = text / javascript><br>
 setInterval(<br>
   功能()<br>
   {<br>
      $ .getJSON(<br>
         $ SCRIPT_ROOT +&#39;_update_USDJPY&#39;,<br>
         {},<br>
         功能(数据)<br>
         {<br>
           $(“#post”)。text(data.post);</p>

<p>         });<br>
   },<br>
   500);</p>

<p>&lt;/ script&gt;</p>

<p>{%封锁内容%}<br>
     {%,占帖子总数的%}<br>
         <strong>询问:&lt;/ strong&gt;:{{post.Ask}}<br>
         <strong>出价:&lt;/ strong&gt;:{{post.Bid}}<br>
         <br><br>
     {%endfor%}<br>
 {%endblock%}<br>
 &lt;/ code&gt; &lt;/ pre&gt;</p>

<p><p>谢谢。&lt;/ p&gt;<br>
     &lt;/ div&gt;</p>

展开原文

原文

It is my first post here I always find useful information but this time my turn to be dry!

I am trying to display and refresh the data bid & ask on the html , the data are generated in a python module . I am trying to do that with jQuery and Ajax.

Here is the JSON I am sending, to be more precise I intend to send a list with each pair of currencies, here a single entry for USDJPY.

{'Bid': 109.355, 'Ask': 109.368}

Here is the python code:

import oandapy
from flask import Flask,render_template,request,jsonify

app = Flask(__name__)

def getspot():
    oanda = oandapy.API(environment="practice", access_token="XXXXX")

    response = oanda.get_prices(instruments="USD_JPY")

    prices = response.get("prices")

    #posts =  {"Bid": biding_price,"Ask" : asking_price}
    posts = [dict(Bid=prices[0].get("bid"),Ask=prices[0].get("ask"))]

    return posts


@app.route('/_update_USDJPY',methods=["GET"]) 
def getUSDJY():

    return jsonify(getspot())

@app.route('/',methods=["GET"]) 
def index():

    posts = getspot()

    return render_template("basic2.html")

if __name__=="__main__":
    app.run()

Here is my html page:

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>

<script type=text/javascript>
setInterval(                               
  function()
  {
     $.getJSON(                            
        $SCRIPT_ROOT + '_update_USDJPY',     
        {},                                
        function(data)                    
        {
          $("#post").text(data.post);                              

        });
  },
  500); 

</script>


{% block content %} 
    {% for post in posts %} 
        <strong>Ask:</strong> {{ post.Ask }} 
        <strong>Bid:</strong> {{ post.Bid }}
        <br>
    {% endfor %} 
{% endblock %} 

Thank you in advance.

1个回答


将脚本标签添加到我们的页面,该脚本标签将全局变量设置为应用程序根目录的前缀。 像这样:</ p>

  <br>
   $ SCRIPT_ROOT = {{request.script_root | tojson | safe}};<br>
 &lt;/ script&gt;<br>
 &lt;/ code&gt; &lt;/ pre&gt;</p>

<p><p>您的JavaScript代码将如下所示:&lt;/ p&gt;</p>

<p><pre> <code> $(function(){<br>
       $ .getJSON($ SCRIPT_ROOT +&#39;/ _update_USDJPY&#39;,{<br>
       },函数(数据){<br>
         $(“#post”)。text(data.post);<br>
       });<br>
       返回false;<br>
     });<br>
   });<br>
 &lt;/ code&gt; &lt;/ pre&gt;</p>

<p><p>在html中:&lt;/ p&gt;</p>

<p><pre> <code> {%块内容%}<br>
   {%,占帖子总数的%}<br>
     <strong>问:&lt;/ strong&gt;:<div id ='post'> {{post}} &lt;/ div&gt;<br>
     <strong>出价&lt;/ strong&gt;:<div id ='post'> {{post}} &lt;/ div&gt;<br>
     <br><br>
   {%endfor%}<br>
 {%endblock%}<br>
 &lt;/ code&gt; &lt;/ pre&gt;</p>

<p><p>这不是经过测试,只是想知道如何实现&lt;/ p&gt;<br>
     &lt;/ div&gt;</p>

展开原文

原文

add a script tag to our page that sets a global variable to the prefix to the root of the application. Something like this:

<script type=text/javascript>
  $SCRIPT_ROOT = {{ request.script_root|tojson|safe }};
</script>

your javascript code would be something like this:

  $(function() {
      $.getJSON($SCRIPT_ROOT + '/_update_USDJPY', {
      }, function(data) {
        $("#post").text(data.post);
      });
      return false;
    });
  });

in html:

{% block content %} 
  {% for post in posts %}
    <strong>Ask:</strong> <div id='post'>{{ post }}</div> 
    <strong>Bid:</strong> <div id='post'>{{ post }}</div>
    <br>
  {% endfor %} 
{% endblock %} 

it's not tested just idea how it can be

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐