Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert Python dictionary to JavaScript hash table?

I have passed to template regular Python dictionary and I need to inside

$(document).ready(function() {.. }

to convert that Python dictionary to JavaScript dictionary. I tried like

var js_dict={{parameters}};

but I got errors ( ' instead of ' and all strings start with u' ). How can I convert Python dictionary to JavaScript hash table ?

like image 651
Damir Avatar asked Apr 09 '12 12:04

Damir


People also ask

What is Python dictionary equivalent in JavaScript?

Python and javascript both have different representations for a dictionary. So you need an intermediate representation in order to pass data between them. The most commonly used intermediate representation is JSON, which is a simple lightweight data-interchange format. The dumps function converts the dict to a string.

Are Python dictionaries implemented as hash tables?

Python dictionaries are implemented as hash tables. Hash tables must allow for hash collisions i.e. even if two distinct keys have the same hash value, the table's implementation must have a strategy to insert and retrieve the key and value pairs unambiguously.

Is Hashtable same as dictionary Python?

Hash tables or has maps in Python are implemented through the built-in dictionary data type. The keys of a dictionary in Python are generated by a hashing function. The elements of a dictionary are not ordered and they can be changed.

Can a dictionary be hashed in Python?

In Python, the Dictionary data types represent the implementation of hash tables. The Keys in the dictionary satisfy the following requirements. The keys of the dictionary are hashable i.e. the are generated by hashing function which generates unique result for each unique value supplied to the hash function.


4 Answers

Python and javascript both have different ideas about how to represent a dictionary, which means that you need a intermediate representation in order to pass data between them. The most common way to do this is JSON, which is a simple lightweight data-interchange format.

Use the python json library to convert (or dump) your python dict into a JSON string. Then in the javascript parse the JSON string into a javascript dict. (If you are using JQuery, then use jQuery.parseJSON)

like image 120
wdh Avatar answered Nov 15 '22 21:11

wdh


You could convert it to JSON and use that in that template

In your python code do

import json
...
...
return {'parameters': json.dumps(parameters)} #This data goes into your template
like image 43
spicavigo Avatar answered Nov 15 '22 23:11

spicavigo


You can use json.dumps(parameters) with mark_safe()

def custom_view(request):
    ...
    return render(request, 'tmpl.html', {'parameters': mark_safe(json.dumps(parameters))})

With mark_safe() I get unescaped code in template.

like image 31
Vivazzi Avatar answered Nov 15 '22 23:11

Vivazzi


I have found that this helps for strings that contain [ ' ] as well

const convertPythonDictToJSON = function (data) {
    let d = data.replace(new RegExp(`(?<=[a-zA-Z])'(?=[a-zA-Z ])`, "g"), '__')
    d = d.replace(new RegExp("'", 'g'), '"')
    d = d.replace(new RegExp("__", 'g'), "'")
    d = d.replace(new RegExp("None", 'g'), 'null')
    d = d.replace(new RegExp("False", 'g'), 'false')
    d = d.replace(new RegExp("True", 'g'), 'true')
    return JSON.parse(d)
}
like image 32
Jackstine Avatar answered Nov 15 '22 22:11

Jackstine