Output your own widget :param template: html template, using `mustache.js `_ syntax :param dict data: Data used to render the template. The data can include the ``put_xxx()`` calls, and the JS function ``pywebio_output_parse`` can be used to
(template: str, data: Dict[str, Any], scope: str = None, position: int = OutputPosition.BOTTOM)
| 1246 | |
| 1247 | @safely_destruct_output_when_exp('data') |
| 1248 | def put_widget(template: str, data: Dict[str, Any], scope: str = None, position: int = OutputPosition.BOTTOM) -> Output: |
| 1249 | """Output your own widget |
| 1250 | |
| 1251 | :param template: html template, using `mustache.js <https://github.com/janl/mustache.js>`_ syntax |
| 1252 | :param dict data: Data used to render the template. |
| 1253 | |
| 1254 | The data can include the ``put_xxx()`` calls, and the JS function ``pywebio_output_parse`` can be used to |
| 1255 | parse the content of ``put_xxx()``. For string input, ``pywebio_output_parse`` will parse into text. |
| 1256 | |
| 1257 | ⚠️:When using the ``pywebio_output_parse`` function, you need to turn off the html escaping of mustache: |
| 1258 | ``{{& pywebio_output_parse}}``, see the example below. |
| 1259 | :param int scope, position: Those arguments have the same meaning as for `put_text()` |
| 1260 | |
| 1261 | :Example: |
| 1262 | |
| 1263 | .. exportable-codeblock:: |
| 1264 | :name: put_widget |
| 1265 | :summary: Use `put_widget()` to output your own widget |
| 1266 | |
| 1267 | tpl = ''' |
| 1268 | <details {{#open}}open{{/open}}> |
| 1269 | <summary>{{title}}</summary> |
| 1270 | {{#contents}} |
| 1271 | {{& pywebio_output_parse}} |
| 1272 | {{/contents}} |
| 1273 | </details> |
| 1274 | ''' |
| 1275 | |
| 1276 | put_widget(tpl, { |
| 1277 | "open": True, |
| 1278 | "title": 'More content', |
| 1279 | "contents": [ |
| 1280 | 'text', |
| 1281 | put_markdown('~~Strikethrough~~'), |
| 1282 | put_table([ |
| 1283 | ['Commodity', 'Price'], |
| 1284 | ['Apple', '5.5'], |
| 1285 | ['Banana', '7'], |
| 1286 | ]) |
| 1287 | ] |
| 1288 | }) |
| 1289 | """ |
| 1290 | spec = _get_output_spec('custom_widget', template=template, data=data, scope=scope, position=position) |
| 1291 | return Output(spec) |
| 1292 | |
| 1293 | |
| 1294 | @safely_destruct_output_when_exp('content') |
no test coverage detected
searching dependent graphs…