Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Question Buttons #1072

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,37 @@

<div class="u-text-align-right">
<button
[style.display]="is_user_question() ? 'none' : 'inline'"
type="button"
class="htf-rounded-button-grey u-push-top"
(click)="sendResponse(response_field)">
{{ hasTextInput() ? 'Submit' : 'Okay' }}
</button>

<button
[style.display]="Button_1() ? 'inline' : 'none'"
type="button"
class="htf-rounded-button-grey u-push-top"
(click)="sendAnswer($event.target.textContent)">
{{Button_1()}}
</button>

<button
[style.display]="Button_2() ? 'inline' : 'none'"
type="button"
class="htf-rounded-button-grey u-push-top"
(click)="sendAnswer($event.target.textContent)">
{{Button_2()}}
</button>

<button
[style.display]="Button_3() ? 'inline' : 'none'"
type="button"
class="htf-rounded-button-grey u-push-top"
(click)="sendAnswer($event.target.textContent )">
{{Button_3()}}
</button>

</div>

</div>
Expand Down
36 changes: 36 additions & 0 deletions openhtf/output/web_gui/src/app/plugs/user-input-plug.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export declare interface UserInputPlugState {
message: string;
'text-input': string;
'image-url': string;
'is-user-question': string;
'button-1-text': string;
'button-2-text': string;
'button-3-text': string;
}

/**
Expand Down Expand Up @@ -99,6 +103,38 @@ export class UserInputPlugComponent extends BasePlug {
return this.getPlugState()['image-url'];
}

is_user_question() {
return (((this.getPlugState()['button-1-text'] !== null) && (this.getPlugState()['button-1-text'].length !== 0))
||((this.getPlugState()['button-2-text'] !== null) && (this.getPlugState()['button-2-text'].length !== 0))
||((this.getPlugState()['button-3-text'] !== null) && (this.getPlugState()['button-3-text'].length !== 0))
);
}


Button_1() {
return this.getPlugState()['button-1-text'];
}

Button_2() {
return this.getPlugState()['button-2-text'];
}

Button_3() {
return this.getPlugState()['button-3-text'];
}


sendAnswer(input: string) {
const promptId = this.getPlugState().id;
let response: string;
if (this.is_user_question()) {
response = input.trim();
} else {
response = '';
}
this.respond('respond', [promptId, response]);
}

sendResponse(input: HTMLInputElement) {
const promptId = this.getPlugState().id;
let response: string;
Expand Down
28 changes: 23 additions & 5 deletions openhtf/plugs/user_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class Prompt(object):
message = attr.ib(type=Text)
text_input = attr.ib(type=bool)
image_url = attr.ib(type=Optional[Text], default=None)
button_1_text = attr.ib(type=Optional[Text], default=None)
button_2_text = attr.ib(type=Optional[Text], default=None)
button_3_text = attr.ib(type=Optional[Text], default=None)


class ConsolePrompt(threading.Thread):
Expand Down Expand Up @@ -156,7 +159,11 @@ def _asdict(self) -> Optional[Dict[Text, Any]]:
return {
'id': self._prompt.id,
'message': self._prompt.message,
'text-input': self._prompt.text_input
'text-input': self._prompt.text_input,
'image-url': self._prompt.image_url,
'button-1-text': self._prompt.button_1_text,
'button-2-text': self._prompt.button_2_text,
'button-3-text': self._prompt.button_3_text
}

def tearDown(self) -> None:
Expand All @@ -176,7 +183,10 @@ def prompt(self,
text_input: bool = False,
timeout_s: Union[int, float, None] = None,
cli_color: Text = '',
image_url: Optional[Text] = None) -> Text:
image_url: Optional[Text] = None,
button_1_text: Optional[Text] = None,
button_2_text: Optional[Text] = None,
button_3_text: Optional[Text] = None) -> Text:
"""Display a prompt and wait for a response.

Args:
Expand All @@ -185,6 +195,7 @@ def prompt(self,
timeout_s: Seconds to wait before raising a PromptUnansweredError.
cli_color: An ANSI color code, or the empty string.
image_url: Optional image URL to display or None.
button_x_text: Optional. Show up to 3 buttons. The button text is returned when clicked

Returns:
A string response, or the empty string if text_input was False.
Expand All @@ -193,21 +204,25 @@ def prompt(self,
MultiplePromptsError: There was already an existing prompt.
PromptUnansweredError: Timed out waiting for the user to respond.
"""
self.start_prompt(message, text_input, cli_color, image_url)
self.start_prompt(message, text_input, cli_color, image_url,button_1_text,button_2_text,button_3_text)
return self.wait_for_prompt(timeout_s)

def start_prompt(self,
message: Text,
text_input: bool = False,
cli_color: Text = '',
image_url: Optional[Text] = None) -> Text:
image_url: Optional[Text] = None,
button_1_text: Optional[Text] = None,
button_2_text: Optional[Text] = None,
button_3_text: Optional[Text] = None) -> Text:
"""Display a prompt.

Args:
message: A string to be presented to the user.
text_input: A boolean indicating whether the user must respond with text.
cli_color: An ANSI color code, or the empty string.
image_url: Optional image URL to display or None.
button_x_text: Optional. Show up to 3 buttons. The button text is returned when clicked

Raises:
MultiplePromptsError: There was already an existing prompt.
Expand All @@ -228,7 +243,10 @@ def start_prompt(self,
id=prompt_id,
message=message,
text_input=text_input,
image_url=image_url)
image_url=image_url,
button_1_text=button_1_text,
button_2_text=button_2_text,
button_3_text=button_3_text)
if sys.stdin.isatty():
self._console_prompt = ConsolePrompt(
message, functools.partial(self.respond, prompt_id), cli_color)
Expand Down