« first day (3070 days earlier)      last day (2103 days later) » 
00:00 - 22:0023:00 - 00:00

23:24
Hello I am learning to use python Flask module and was wondering if anybody here can help me solve an issue:

I am attempting to pass a dictionary via the render_template() function, and on the template I am using it to instantiate a Javascript variable with a value, i.e.:
`{% block scripts %}
<script> var test = {{dict}} </script>
{% endblock %}`
Immediate hunch, you need {{ dict|safe }}
render template:
`return render_template('index.html', dict=json.dumps(dict))`
And as a side note, you really shouldn't be using dict as a name
@roganjosh oh so just adding safe will fix it? aso is it right that i am using json.dumps to pass it?
Without seeing the whole project (I don't think we need to see it) that would be my first guess
23:28
oh sorry i used the name dict here to make my example easier to understand, sorry
No need to apologise :)
Have a go with |safe and see if it works. If it doesn't, we can try to debug from there
if i put {{dict}} outside of the script tag its showing the json data, but even puting {{dict|safe}} doesnt seem to work
wait let me try JSON.parse()
You shouldn't need JSON.parse
Get rid of json.dumps() on the python end and just send the dictionary
ok thanks let me try :)
If it doesn't, what do you see from console.log(test); in F12?
23:33
@roganjosh when i just pass the dictionary on the browser inspector i see it generate the script tag as:
<script> var OrigHook = <class 'dict'> </script>
so its passing the python class i guess?
It sounds like you've called str() on your actual dictionary tbh
I'm not sure JS could generate <class 'dict'> on its own, that's str(type(my_dict)) from Python but I could well be wrong
ok let me pass a variable called type and see what it shows when I do type(hook), which is the variable containing the dictionary
I'm getting confused. type is also a python builtin. Can you give some (small) code overview of what you're doing here?
You shouldn't need to call type() on anything
ok so when you navigate to index, I was trying to make an apicall to fetch some json data, and then wanted to instantiate a local javascript variable with the json data
So are we talking an AJAX call to the API?
23:40
No I am doing a python request call
Right, so Flask is going to do a full page refresh
so the server makes the api request, and returns the result to client as a javascript variable as the page loads
yes
And we're trying to pass a named object to the template
Ok, what is the minimal version of the views function that does this?
Whatever the function is that return render_template(...) concerning that view
this is the code:
    @app.route('/')
    @app.route('/index')
    def index():
    	hook = daud.getHook()
    	return render_template('index.html', hook=hook)
Ok, so now your type(hook) comment makes sense to me in terms of that function. Leaving the template part aside, you get dict?
23:44
this is the template:
{% block scripts %}
	<script> var OrigHook = {{dict|safe}} </script>
{% endblock %}
"leaving the template part aside"... :P
@roganjosh yes
oh silly me
Ok, so on the template, you need to be accessing hook, not dict
@roganjosh yes i saw that error, just changed it to hook, still doesnt work :(
i will try json.dumps again
No, don't
we're making some progress step-by-step
23:47
ok
So now you have <script> var OrigHook = {{hook|safe}} </script>
yes i do
% block scripts %}
	<script> var OrigHook = {{hook|safe}} </script>
{% endblock %}
What do you get in the F12 console window from console.log(hook); inside that script?
still not working though :(
Oh lol
I re-ran app.py
and it works :D
sorry I was being a dumb dumb
@roganjosh it works now, just tried it with a differnet api call aswell
thank you soo much :)
You're welcome
23:50
interesting so you can pass a unchanged dictionary, and python will auto convert it to the right format than
pretty awesome :)
It will on a full page render
if I wanted to do a live change, I would have to use AJAX right?
When you want to render parts of the page with AJAX you may way to jsonify the response and have the JQuery function handle that
sorry still quite new to the module, but i find it much more simpler to php
@roganjosh ah okay, yeah that makes sense
A live change can always be done by a full redirect to a route. That's usually ugly, so you render portions of the page
23:54
guess I gott to add an option on my class which makes the api request and fetches the data, to return either as dictionary or as jsonified
@roganjosh haha sounds like something cool to learn, but I guess I should carry on learning the basics for now :)
<div id="update_me">{ include 'my_template.html' }</div> and have AJAX send a request to a Flask route that render_template() with just my_template.html and the data necessary to populate it.
Ohh
thats sounds cool :)
it sounds like a feature called lazy loading, is that correct?
haha sorry dont want to bug you too much, I have been learning the module from this playlist: youtu.be/UIJKdCIEXUQ?list=PL-osiE80TeTs4UjLw5MM6OjgkjFeUxCYH, so I guess I should carry on :)
Honestly, I don't know what "lazy loading" is
@roganjosh thank you very much, bye :)
00:00 - 22:0023:00 - 00:00

« first day (3070 days earlier)      last day (2103 days later) »