-1

I was trying to get data from "byp" and "yp1" on HTML page by Flask but I could not.

As well I want to print in html page both print contents.

I was trying to define a new route page and to import by using Jinja commands like {{ byp }} and {{ yp1 }} but did not worked I am new in field and I am sure there is something to be done before ...

The code is working 100%.

Can anyone help me with a hint?

    import requests, json
    import datetime
    import time

@app.route('/bpay/' + token_urlsafe(12))
def getBitcoinPrice():
    URL = 'https://www.bitstamp.net/api/ticker/'
    try:
        r = requests.get(URL)
        priceFloat = float(json.loads(r.text)['last'])
        bp = getBitcoinPrice()
        yp = 12
        byp = yp / bp
        yp1 = bp * byp
        btc = '1F1tAaz5x1HUXrCNLbtMDqcw6o5GNn4xqX'

        return render_template('a/bpay.html', byp=byp, yp1=yp1, bp=bp)

    except requests.ConnectionError:
        print ('Error querying Bitstamp API')

So, I was trying this code:

URL = os.popen("GET https://www.bitstamp.net/api/ticker/").read()
    for last in URL:
        r = requests.get(URL)
        pf = float(json.loads(r.text)['last'])
    bp = pf
    yp = 12
    byp = yp / bp
    yp1 = bp * byp

And I was getting this error:

 Traceback (most recent call last):
  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask_login/utils.py", line 228, in decorated_view
    return func(*args, **kwargs)
  File "/home/ubuntu/workspace/n1/app.py", line 1480, in btpay
    r = requests.get(URL)
  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/requests/api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/requests/sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/requests/sessions.py", line 612, in send
    adapter = self.get_adapter(url=request.url)
  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/requests/sessions.py", line 703, in get_adapter
    raise InvalidSchema("No connection adapters were found for '%s'" % url)
requests.exceptions.InvalidSchema: No connection adapters were found for '{"high": "8160.85000000", "last": "8099.47", "timestamp": "1523975088", "bid": "8093.02", "vwap": "8062.56", "volume": "7016.85906762", "low": "7938.78000000", "ask": "8099.47", "open": 8054.68}'
alex
  • 1
  • 4
  • Hello, were you able make it work? – BeeBee8 Apr 17 '18 at 06:38
  • @BhushanBabar did not worked I do not know how to do it or something is missing I found so many materials on internet related to json what are not easy for me to understand now at my level :(. I was finding an alternative to add the values manual instead to get them updated is helping me but is not what I was expected ... maybe I am thinking more than my knowledge ... – alex Apr 17 '18 at 07:43

1 Answers1

1

I have been practicing flask for last few months. I hope this may help you. I tried to make it simple and easy to understand for you because you are new to Flask.

Use render_template to connect your route function with html template ( make sure html template is in templates directory.

BackEnd

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/test')
def test():
    text = 'Hello World'
    return render_template('test.html', text=text)

if __name__ == '__main__':
    app.jinja_env.auto_reload = True
    app.config['TEMPLATES_AUTO_RELOAD'] = True
    app.debug = True
    app.run(host='0.0.0.0', port=5000)

Inside Jinja template, to print the variable came from backend

{{ text }}
  • thank you but I did like that and it does not working :( – alex Apr 16 '18 at 17:32
  • @alex Update question with the code you have tried. We can not know what's not working until you do that. – BeeBee8 Apr 16 '18 at 17:39
  • @BhushanBabar the ideea is I was trying exactly how Masud Rumir did on his example and is not working is not appearing the data but more even none errors ... – alex Apr 16 '18 at 17:42
  • Please check the proper indention in python code. May the indentation is not ok in my example code. It should work . – Masud Rumii Apr 16 '18 at 17:44
  • @alex Did his example work without your modification? – BeeBee8 Apr 16 '18 at 17:45
  • @BhushanBabar I was updating now, yes I did but not working yet even no errors not 500 not 400 – alex Apr 16 '18 at 17:46
  • I just tried to show you an example how you can pass a variable to html template. – Masud Rumii Apr 16 '18 at 17:46
  • @alex try to send object instead of multiple values. Like I have demonstrated in this answer. https://stackoverflow.com/questions/49590870/how-can-i-pass-json-of-flask-to-javascript/49591420#49591420 – BeeBee8 Apr 16 '18 at 17:49
  • @BhushanBabar thank you very much for your quick and very good answer! – alex Apr 16 '18 at 17:52
  • I have updated the code . It should work now if you just copy and paste. Please let me know. – Masud Rumii Apr 16 '18 at 17:53
  • @BhushanBabar In 10 mins I will let you know I am coding my code on your way but I am 100% will work – alex Apr 16 '18 at 17:55
  • @alex I have just tested my code and it works . Run the app.py script and then go to browser and visit localhost:5000/test. Don't forget to add {{ text }} in html template call 'test.html' in templates directory. – Masud Rumii Apr 16 '18 at 17:58
  • @BhushanBabar no is not working is giving me errors for priceFloat, bp, byp, yp1 something like that "byp" : "yp / bp" ^ SyntaxError: invalid syntax – alex Apr 16 '18 at 18:12
  • @alex maybe your division syntax is wrong , try to read error properly. Refer this https://stackoverflow.com/questions/6315865/how-can-i-divide-two-integers-stored-in-variables-in-python – BeeBee8 Apr 16 '18 at 18:15
  • You forgot to import Flask, render_template – Masud Rumii Apr 16 '18 at 18:17
  • I will try now and I will let you know asap – alex Apr 16 '18 at 18:18
  • @MasudRumii it is imported because the code is long and is included the render_template – alex Apr 16 '18 at 18:18
  • @alex try to do it with hard coded values first, maybe also try with integers that are divisible by denominator. – BeeBee8 Apr 16 '18 at 18:22
  • I am trying now all the alternatives and once I am done well I will let you know I will stay on it till I will done it ... I will update asap – alex Apr 16 '18 at 18:26
  • @alex `bp = getBitcoinPrice()` are you sure this line is working? Bcoz you are using same function name here `def getBitcoinPrice():` I'm signing off , hope I have given you enough clues. Good luck. Don't forget to upvote the answer if it works for you. – BeeBee8 Apr 16 '18 at 18:29
  • @BhushanBabar in default code it was working now I will check .. thank you – alex Apr 16 '18 at 18:32