Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 - Allows for an `other_text` option inside `unlisted_choice` which allows user to specify a custom text in the select drop-down
 - Hide select drop-down and label if no choices are defined
  • Loading branch information
batpad committed Sep 2, 2023
1 parent dc119c2 commit e041fab
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
1 change: 1 addition & 0 deletions jupyterhub_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
'unlisted_choice': {
'enabled': True,
'display_name': 'Image Location',
'other_text': 'Enter Image manually',
'validation_regex': '^pangeo/.*$',
'validation_message': 'Must be a pangeo image, matching ^pangeo/.*$',
'kubespawner_override': {'image': '{value}'},
Expand Down
17 changes: 10 additions & 7 deletions kubespawner/spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3143,19 +3143,22 @@ async def _load_profile(self, slug, profile_list, selected_profile_user_options)
# Get selected options or default to the first option if none is passed
for option_name, option in profile.get('profile_options').items():
unlisted_choice_form_key = f'{option_name}--unlisted-choice'
has_unlisted_choice_key = (option.get('unlisted_choice', {}).get('enabled', False)
and unlisted_choice_form_key in selected_profile_user_options)
has_unlisted_choice_value = (option.get('unlisted_choice', {}).get('enabled', False)
and unlisted_choice_form_key in selected_profile_user_options
and selected_profile_user_options[unlisted_choice_form_key] != '')
chosen_option = selected_profile_user_options.get(option_name, None)
# If none was selected get the default. At least one choice is
# guaranteed to have the default set
if not chosen_option:
for choice_name, choice in option['choices'].items():
if choice.get('default', False):
chosen_option = choice_name
if not has_unlisted_choice_value:
for choice_name, choice in option['choices'].items():
if choice.get('default', False):
chosen_option = choice_name

# Handle override for unlisted_choice free text specified by user
if (
option.get('unlisted_choice', {}).get('enabled', False)
and unlisted_choice_form_key in selected_profile_user_options
):
if has_unlisted_choice_key:
chosen_option_overrides = option['unlisted_choice'][
'kubespawner_override'
]
Expand Down
27 changes: 19 additions & 8 deletions kubespawner/templates/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,30 @@ <h3>{{ profile.display_name }}</h3>
<div class="js-options-container">
<div class="option">
<label for="profile-option-{{ profile.slug }}-{{ k }}"
class="js-profile-option-label">{{ option.display_name }}</label>
class="js-profile-option-label {%- if not option['choices'] %} hidden {%- endif %}">
{{ option.display_name }}
</label>
<select name="profile-option-{{ profile.slug }}-{{ k }}"
class="form-control js-profile-option-select">
{%- for k, choice in option['choices'].items() %}
<option value="{{ k }}" {% if choice.default %}selected{% endif %}>{{ choice.display_name }}</option>
{%- endfor %}
class="form-control js-profile-option-select {%- if not option['choices'] %} hidden {%- endif %}">
{%- if option['choices'] %}
{%- for k, choice in option['choices'].items() %}
<option value="{{ k }}" {% if choice.default %}selected{% endif %}>{{ choice.display_name }}</option>
{%- endfor %}
{%- endif %}
{%- if option['unlisted_choice'] and option['unlisted_choice']['enabled'] %}
<option value="unlisted-choice">Other...</option>
<option value="unlisted-choice">
{%- if option['unlisted_choice']['other_text'] %}
{{ option['unlisted_choice']['other_text'] }}
{%- else %}
Other...
{%- endif %}
</option>
{%- endif %}
</select>
</select>
</div>
{%- if option['unlisted_choice'] and option['unlisted_choice']['enabled'] %}
<div class="option hidden js-other-input-container">
<div class="option js-other-input-container
{%- if option['choices'] %} hidden {%- endif %}">
<label for="profile-option-{{ profile.slug }}-{{ k }}--unlisted-choice">
{{ option['unlisted_choice']['display_name'] }}
</label>
Expand Down

0 comments on commit e041fab

Please sign in to comment.