You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/user_guide/Parameters.ipynb
+100Lines changed: 100 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -40,6 +40,7 @@
40
40
"- **precedence**: Optional numeric value controlling whether this parameter is visible in a listing and if so in what order.\n",
41
41
"- **allow_refs**: Whether to allow the Parameter to accept references to other Parameters that will be dynamically resolved.\n",
42
42
"- **nested_refs**: Whether references should be resolved even when they are nested inside a container.\n",
43
+
"- **metadata**: Optional arbitrary mapping, to be used to store additional metadata than cannot be declared with the other attributes. Useful for third-party libraries to extend Param.\n",
43
44
"\n",
44
45
"Most of these settings (apart from **name**) are accepted as keyword arguments to the Parameter's constructor, with `default` mostly also accepted as the only positional argument:"
45
46
]
@@ -929,6 +930,105 @@
929
930
"Object().debug_id, SubObject().debug_id"
930
931
]
931
932
},
933
+
{
934
+
"cell_type": "code",
935
+
"execution_count": null,
936
+
"id": "b4a9751f-e307-4b9e-8c25-fc2059200152",
937
+
"metadata": {},
938
+
"outputs": [],
939
+
"source": [
940
+
"import param"
941
+
]
942
+
},
943
+
{
944
+
"cell_type": "markdown",
945
+
"id": "32cc7b01-55a2-43dd-b886-acf0c30cb5ba",
946
+
"metadata": {},
947
+
"source": [
948
+
"## Arbitrary Metadata\n",
949
+
"\n",
950
+
"Parameters may include arbitrary metadata as a mapping stored in the `metadata` attribute (default is `None`). This attribute is not used internally by Param, instead it is meant to enable rich functionality in third-party libraries, or to be leveraged in your own code base to extend Parameters with information that cannot be declared with the other attributes available. Note that a shallow copy of the mapping you'll pass will be made on class creation and on instance-level parameter creation. This behavior is not specific to the `metadata` attribute, it is how Param handles attributes with value that are mutable containers (lists, dicts, sets)."
951
+
]
952
+
},
953
+
{
954
+
"cell_type": "code",
955
+
"execution_count": null,
956
+
"id": "cf82e531-40f4-4960-9ba1-1df012711a93",
957
+
"metadata": {},
958
+
"outputs": [],
959
+
"source": [
960
+
"extra_info = {'library': {'config': True}}\n",
961
+
"\n",
962
+
"class P(param.Parameterized):\n",
963
+
" s = param.String(metadata=extra_info)"
964
+
]
965
+
},
966
+
{
967
+
"cell_type": "code",
968
+
"execution_count": null,
969
+
"id": "c9f6182e-320d-4e54-b8aa-7596ccabdb29",
970
+
"metadata": {},
971
+
"outputs": [],
972
+
"source": [
973
+
"P.param['s'].metadata"
974
+
]
975
+
},
976
+
{
977
+
"cell_type": "code",
978
+
"execution_count": null,
979
+
"id": "9207c2f0-44de-4bed-8f80-753393fcf66f",
980
+
"metadata": {},
981
+
"outputs": [],
982
+
"source": [
983
+
"p = P()\n",
984
+
"p.param.s.metadata"
985
+
]
986
+
},
987
+
{
988
+
"cell_type": "code",
989
+
"execution_count": null,
990
+
"id": "639dadc6-bf31-4eac-9327-8bcc12e0eb63",
991
+
"metadata": {},
992
+
"outputs": [],
993
+
"source": [
994
+
"P.param['s'].metadata is extra_info # shallow copy made on class creation"
995
+
]
996
+
},
997
+
{
998
+
"cell_type": "code",
999
+
"execution_count": null,
1000
+
"id": "d3cd5940-9b40-48a3-9fe1-4a14f2c6c803",
1001
+
"metadata": {},
1002
+
"outputs": [],
1003
+
"source": [
1004
+
"p.param['s'].metadata is P.param['s'].metadata # shallow copy made when the instance-level Parameter is created"
1005
+
]
1006
+
},
1007
+
{
1008
+
"cell_type": "markdown",
1009
+
"id": "d0cfdb8c-29ac-4391-a0c8-353665e93e03",
1010
+
"metadata": {},
1011
+
"source": [
1012
+
"To provide a more concrete example on how third-party libraries can benefit from the `metadata` attribute, we'll give an example with [Panel](https://panel.holoviz.org), a library to build web apps written in Python only. Panel already integrates with Param, and makes it easy to display a Parameterized instance as an app component, each Parameter type being automatically mapped to a widget type (e.g., a `Number` Parameter is displayed as a float slider). Customizing the automatically created widget requires additional work in Panel (e.g., using the `Widget.from_param(<parameter>)` API). To simplify this process, Panel could inspect the `metadata` attribute for custom widget configuration, and apply it on widget creation:"
0 commit comments