通过烧瓶显示我的Web应用程序中的动态更改数据

时间:2017-10-31 22:45:05

标签: python python-3.x web flask pythonanywhere

我想在我的网络应用上显示来自请求API的一些动态变化数据 我的要求是:

 url='https://bittrex.com/api/v1.1/public/getmarketsummary?market=btc-nav'
  z=[]
  r = requests.get(url)
  x=r.json()
  e1=x['result'][0]['Ask']

我想在pythonanywhere的web应用程序中显示它 我有这样的html文件:

<!DOCTYPE html>

<html>

<head>
    <title>Suggestions</title>
</head>

<body>

Search: <input type="text" id="search_form_input"></input>

<div id="place_for_suggestions"></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<script>
$("#search_form_input").keyup(function(){
    var text = $(this).val();

    $.ajax({
      url: "/suggestions",
      type: "get",
      data: {jsdata: text},
      success: function(response) {
        $("#place_for_suggestions").html(response);
      },
      error: function(xhr) {
        //Do Something to handle error
      }
    });
});
</script>

</body>

</html>

我的烧瓶文件:

from flask import Flask, render_template, request
import requests


app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/suggestions')
def suggestions():
    text = request.args.get('jsdata')

    suggestions_list = []

    if text:
         url='https://bittrex.com/api/v1.1/public/getmarketsummary?market=btc-nav'
         z=[]
         r = requests.get(url)
         x=r.json()
         e1=x['result'][0]['Ask']
         suggestions=e1
         for suggestion in suggestions:
            suggestions_list.append(suggestion.attrs['data'])

        #print(suggestions_list)

    return render_template('suggestions.html', suggestions=suggestions_list)


if __name__ == '__main__':
    app.run(debug=True)

我收到此错误:

Internal Server Error

suggestions.html:

<label id="value_lable">
    {% for suggestion in suggestions %}
        {{ suggestion }}<br>
    {% endfor %}
</label>
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

我认为与ajax有关。请帮我。我不知道如何修复它以及如何更新来自https://bittrex.com/api/v1.1/public/getmarketsummary?market=btc-nav的数据,例如每2-3秒更新一次

1 个答案:

答案 0 :(得分:1)

在这种情况下我做了什么:

更改此代码:

$.ajax({
  url: "/suggestions",
  type: "get",
  data: {jsdata: text},
  success: function(response) {
    $("#place_for_suggestions").html(response);
  },
  error: function(xhr) {
    //Do Something to handle error
  }
});

和此:

@app.route('/suggestions')
def suggestions():
    text = request.args.get('jsdata')

    suggestions_list = []

    if text:
         url='https://bittrex.com/api/v1.1/public/getmarketsummary?market=btc-nav'
         z=[]
         r = requests.get(url)
         x=r.json()
         e1=x['result'][0]['Ask']
         suggestions=e1
         for suggestion in suggestions:
            suggestions_list.append(suggestion.attrs['data'])

        #print(suggestions_list)

    return render_template('suggestions.html', suggestions=suggestions_list)

用这个:

$.ajax({
    url: "/suggestions",
    type: "get",
    data: {jsdata: text},
    success: function (response) {
        $("#place_for_suggestions").append($($.parseJSON(response)).each(function (index, item) {
            $('<div>').text(item).append($('<br>'))
        }));
    },
    error: function (xhr) {
        //Do Something to handle error
    }
});

并建议返回json

return jsonify(suggestions_list)