How can I load a JSON file in Brython?

187 Views Asked by At

I am working on a page with Brython, and one thing I would like to do is load a JSON file on the same server, in the same directory.

How can I do that? open()? Something else?

Thanks,

1

There are 1 best solutions below

2
Micka108 On BEST ANSWER

You are on the right track, here is how I did it.

In my file .bry.

import json

css_file = "static/json/css.json"

j = json.load(open(css_file))

# For debug
console.log(j)

Since it is from your HTML page that the execution is done, make sure to start from your .html file and not .py or .bry to search your JSON file.

Also, check if your JSON file has the right structure ;)

Here is my tree structure.

|   index.html
|   README.txt
|   unicode.txt
|   
+---static
|   +---assets
|   |       
|   +---css
|   |           
|   +---fonts
|   |                      
|   +---js
|   |         
|   +---json
|   |       css.json
|   |       
|   \---py
|       |   main.bpy

Hoping to have helped :D