gradio/demo/hangman/run.py
Aarni Koskela ef3862e075
Switch linting to Ruff (#3710)
* Sort requirements.in

* Switch flake8 + isort to ruff

* Apply ruff import order fixes

* Fix ruff complaints in demo/

* Fix ruff complaints in test/

* Use `x is not y`, not `not x is y`

* Remove unused listdir from website generator

* Clean up duplicate dict keys

* Add changelog entry

* Clean up unused imports (except in gradio/__init__.py)

* add space

---------

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
2023-04-03 15:48:18 -07:00

35 lines
1.0 KiB
Python

import gradio as gr
secret_word = "gradio"
with gr.Blocks() as demo:
used_letters_var = gr.State([])
with gr.Row() as row:
with gr.Column():
input_letter = gr.Textbox(label="Enter letter")
btn = gr.Button("Guess Letter")
with gr.Column():
hangman = gr.Textbox(
label="Hangman",
value="_"*len(secret_word)
)
used_letters_box = gr.Textbox(label="Used Letters")
def guess_letter(letter, used_letters):
used_letters.append(letter)
answer = "".join([
(letter if letter in used_letters else "_")
for letter in secret_word
])
return {
used_letters_var: used_letters,
used_letters_box: ", ".join(used_letters),
hangman: answer
}
btn.click(
guess_letter,
[input_letter, used_letters_var],
[used_letters_var, used_letters_box, hangman]
)
if __name__ == "__main__":
demo.launch()