egorkh
egorkh

Reputation: 488

show dynamically changing data in my web app by flask

i want to show some dynamically changing data from request api on my web app my request is :

 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']

i want to display it on web app in pythonanywhere i have html file like this :

<!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>

my flask file:

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)

i get this error :

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.

i think something with ajax. please help me. I have no idea how to fix it and how to update data from https://bittrex.com/api/v1.1/public/getmarketsummary?market=btc-nav dinamicly changing every 2-3 seconds for example

Upvotes: 1

Views: 2251

Answers (1)

Yakir Tsuberi
Yakir Tsuberi

Reputation: 247

What i do in that case:

change this code:

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

and this:

@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)

with this:

$.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
    }
});

and suggestions return json

return jsonify(suggestions_list)

Upvotes: 1

Related Questions