Update attributes of input field. This function can only be called in ``onchange`` callback of input functions. :param str name: The ``name`` of the target input item. Optional, default is the name of input field which triggers ``onchange`` :param spec: The input parameters need
(name: str = None, **spec)
| 763 | |
| 764 | |
| 765 | def input_update(name: str = None, **spec): |
| 766 | """Update attributes of input field. |
| 767 | This function can only be called in ``onchange`` callback of input functions. |
| 768 | |
| 769 | :param str name: The ``name`` of the target input item. |
| 770 | Optional, default is the name of input field which triggers ``onchange`` |
| 771 | :param spec: The input parameters need to be updated. |
| 772 | Note that those parameters can not be updated: |
| 773 | ``type``, ``name``, ``validate``, ``action``, ``code``, ``onchange``, ``multiple`` |
| 774 | |
| 775 | An example of implementing dependent input items in an input group: |
| 776 | |
| 777 | .. exportable-codeblock:: |
| 778 | :name: input-update |
| 779 | :summary: Dependent input items in input group |
| 780 | |
| 781 | country2city = { |
| 782 | 'China': ['Beijing', 'Shanghai', 'Hong Kong'], |
| 783 | 'USA': ['New York', 'Los Angeles', 'San Francisco'], |
| 784 | } |
| 785 | countries = list(country2city.keys()) |
| 786 | location = input_group("Select a location", [ |
| 787 | select('Country', options=countries, name='country', |
| 788 | onchange=lambda c: input_update('city', options=country2city[c])), |
| 789 | select('City', options=country2city[countries[0]], name='city'), |
| 790 | ]) |
| 791 | put_text(location) # ..demo-only |
| 792 | """ |
| 793 | task_id = get_current_task_id() |
| 794 | k = 'onchange_trigger-' + task_id |
| 795 | if k not in get_current_session().internal_save: |
| 796 | raise RuntimeError("`input_update()` can only be called in `onchange` callback.") |
| 797 | trigger_name = get_current_session().internal_save[k] |
| 798 | |
| 799 | if name is None: |
| 800 | name = trigger_name |
| 801 | |
| 802 | attributes = parse_input_update_spec(spec) |
| 803 | |
| 804 | send_msg('update_input', dict(target_name=name, attributes=attributes)) |
nothing calls this directly
no test coverage detected
searching dependent graphs…