Google Colab 表单作为字典

Google Colab form as a dictionary

可不可以有一个可以给出字典的google colab form?比如填表的时候结果是字典变量?

#@title Authorization Form
<key1> = "value1" #@param {type:"string"}
<key2> = "value2" #@param {type:"string"}

上面的键值对应该会自动放入字典中,而不仅仅是独立的字符串。谢谢!

一种解决方法是将表单放入函数中,然后调用 locals() 以获取函数局部变量的字典。

def form_dict():

    # @title Authorization Form
    Name = 'John Doe'    # @param {type: "string"}
    Age = 42    # @param {type: "integer"}

    return locals()


print(form_dict())

输出:

{'Name': 'John Doe', 'Age': 42}

Colab notebook