javascript - 如何使用 python json 将模板渲染为 TreeView ?

标签 javascript python python-3.x flask jinja2

我需要用flask渲染一个html模板。 我有一个 python 字典,我想在 TreeView 中显示它。 我该怎么做?

我找到了这个资源:https://github.com/jonmiles/bootstrap-treeview 这可能很有用,但我不能使用它。

dict_var = {
    "test3@test3.com": {
        "discord_id": 0,
        "status_key": False,
        "username": "test3@test3.com",
        "last_name": "aaaa",
        "gender": "male",
        "email": "test3@test3.com",
    },
    "test9@test9.com": {
        "username": "test9@test9.com",
        "last_name": "test9",
        "gender": "male",
        "discord_data": {
            "user": {
                "avatar": None,
                "discriminator": "111",
                "verified": True,
            },
            "connections": [],
            "guilds": [
                {
                    "icon": "hellohellohellohello.jpg",
                    "id": "222222222222",
                },
                {
                    "icon": None,
                    "id": "111111111",
                    "owner": True,
                }
            },
            "email": "hello@hello.com",
            "activation_key": "hello-hello-hello-hello-hello",
            "first_name": "hello@hello.com",
            "discord_id": 22222223333334444,
            "state": "IT"
        }
    }

渲染模板的flask函数

@app.route('/test')
def test():
   return render_template('test.html', res=dict_var)


测试.html

...

<div class="container">
    <div id="tree"></div>

<script>
    $(function() {
  var mytree = JSON.parse('{{ res|safe }}');

  $('#tree').treeview({
    data: mytree
  });
});
</script>


...

如何将res传递给js并将dict转换为treeview?

最佳答案

您可以使用tojson

...


<div class="container">
    <div id="tree"></div>

<script>
    $(function() {
  var mytree = JSON.parse('{{ res|tojson }}');

  $('#tree').treeview({
    data: mytree
  });
});
</script>


...

关于javascript - 如何使用 python json 将模板渲染为 TreeView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55345443/

相关文章:

javascript - JS 全局变量未定义

javascript - 将回调函数和参数传递给 javascript 函数

javascript - 检测另一个框架中存在的按钮的 onClick

python - 访问剪贴板 Windows (7) 64 位

python-3.x - 在 sklearn 中对 pickle 数据进行预测

javascript - 检测下拉列表是否为多选

python - TensorFlow和Keras上的引物: The past (TF1) the present (TF2)

Python正确有效地获取DBUS对象路径(例如/org/bluez/dev_XX_XX_XX_XX_XX_XX/playerY)

python - Luhn 算法在 (I)python 中的意外表现

python - 创建包含月份中的天数的列表