0

I want to show data from request https://bittrex.com/api/v1.1/public/getmarketsummary?market=btc-nav' on my web app. I want this data to update on my app lets say every 3 seconds. I am using flask 3.6 and python 3.6. My flask.py file :

from flask import Flask, render_template, request
import requests



app = Flask(__name__)


@app.route('/', methods=['GET', 'POST'])
def price():
     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']
     e2=x['result'][0]['Bid']
     z.append(e1)
     z.append(e2)
     #print("TEMPLATE_FOLDER = {}".format(app.template_folder))
     return render_template('templ.html', data=z ,template_folder='/home/...templates/')




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

this is my templ.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: "/",
      type: "get",
      data: <label id="value_lable">
        {% for i in data %}
        {{ i }}<br>
        {% endfor %}
        </label>,
      success: function(response) {
        $("#place_for_suggestions").html(response);
      },
      error: function(xhr) {
        //Do Something to handle error
      }
    });
});
</script>

</body>

</html>

I got website without currency price data -http://weblanss.pythonanywhere.com//

I am new in flask, please help me. What should I do to show data from a request on my site? how to update it every 3 sec?

Tom Cupis
  • 316
  • 3
  • 24
egorkh
  • 478
  • 8
  • 24
  • Can you be more specific about what data you want to show? e.g. Do you want the data available for requests to update every 3 seconds, or do you want the data on the client side that has been sent to the client as a response to a request to update every 3 seconds. If you want the data sent and received on the client side to update every 3 seconds, then I believe the best procedure would be to do this with a client side script using JavaScript that makes requests automatically to your Flask App. Essentially, it's just a matter of having proper JavaScript send requests to your Flask App. – David John Coleman II Nov 03 '17 at 23:46
  • @ David John Coleman II I want the data sent and received on the client side to update , i think it will be better solution. i am not familiar with JavaScript .Could you give me some example please? – egorkh Nov 04 '17 at 10:32
  • Your template has JavaScript in it, (JQuery to be specific), and there is an ajax request in it `$.ajax({url: "/", type: "get",`. This is a great starter for you. My suggestion would be to learn how `ajax` requests work, then determine the proper `url` for the request, and the proper `type`, which will most likely be a `get` request. Then, you simply need to create a system that does what you want; i.e. makes that requests every 3 seconds and responds automatically by updating a data field or give user a message, or whatever? This assumes your Flask app serves content properly as well. – David John Coleman II Nov 05 '17 at 14:52
  • Also, keep in mind, at this point, your original question has now become so complex that you are basically asking someone to build an application for you. It is not likely you will find any responses like that on StackOverflow. I suggest you do a lot of reading about JavaScript and Flask, and when you have a more specific question, to come back here. – David John Coleman II Nov 05 '17 at 14:56
  • Also, by the way, I just realized that your template code, uses the ajax request, to make a request to your Flask App, which then makes a get request to another specified URL, does some stuff, then returns a response. There is no need to have the middle step of your Flask App making that request. Your `Ajax` request can go directly to the URL you have: https://bittrex.com/api/v1.1/public/getmarketsummary?market=btc-nav – David John Coleman II Nov 05 '17 at 14:58
  • 1
    However, if you want to keep your setup, and your system is working now, then just automate that `ajax` request on a timer. You can read about that here: https://stackoverflow.com/questions/5052543/how-to-fire-ajax-request-periodically But I suggest your `ajax` request goes directly to bittrex.com not your flask app, as I mentioned above. – David John Coleman II Nov 05 '17 at 15:17
  • @David John Coleman II thanks a lot – egorkh Nov 06 '17 at 17:47

0 Answers0