Skip to content
This repository has been archived by the owner on Dec 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from phorward/api-improvements
Browse files Browse the repository at this point in the history
API improvements
  • Loading branch information
ciansen authored Feb 28, 2023
2 parents 7cc1272 + 217809a commit 4ff3156
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 36 deletions.
86 changes: 52 additions & 34 deletions src/assets/scriptor/dialog.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
"""
Dialogs API.
This module provides several dialogs to be used for several user-interactions.
- alert: Show a modal message to be confirmed
- confirm: Show a Yes-No or Yes-No-Cancel dialog
- input: Input dialog with various types
- select: Select choice of several options, with multi-selection option.
"""

from .utils import is_pyodide_context

if is_pyodide_context():
Expand All @@ -11,9 +22,9 @@

import time
import datetime
import time
import math


async def wait():
if is_pyodide_context():
while manager.resultValue is None:
Expand All @@ -22,52 +33,61 @@ async def wait():
await asyncio.sleep(250)

async def alert(text: str):
"""
Provide a message and stop program execution until accepted.
"""
if is_pyodide_context():
_self.postMessage(type="alert", text=text)
await wait();
await wait()
manager.reset()
manager.resultValue = None
else:
click.prompt("Press a key")

click.pause("Press any key to continue")

async def confirm(title: str, text: str, cancel: bool = False) -> str:

async def confirm(text: str, *, title: str = "Confirm", allow_cancel: bool = False) -> bool | None:
"""
Provide a Yes-No or Yes-No-Cancel-dialog.
"""
if is_pyodide_context():
_self.postMessage(type="confirm", title=title, text=text, cancel=cancel)
await wait();
tmp = manager.copyResult()
_self.postMessage(type="confirm", title=title, text=text, cancel=allow_cancel)
await wait()
ret = manager.copyResult()
manager.reset()
manager.resultValue = None

return tmp
if ret < 0:
return None
elif ret == 0:
return False

ret = True

else:
value = 0
try:
value = int(click.confirm(text, abort=True))
except:
value = -1
return value
ret = click.confirm(text, abort=allow_cancel)

return ret

# time = true
# datetim-picker local

async def input(text: str, *, title: str = "Input", type: str = "input", use_time: bool = False, empty: bool = False):
"""
Provide a dialog asking for some value.
"""

async def input(title: str, text: str, type: str, use_time: bool = False, empty: bool = False):
if is_pyodide_context():
_self.postMessage(type="input", title=title, text=text, input_type=type, use_time=use_time, empty=empty)
await wait();
await wait()
tmp = manager.copyResult()
manager.reset()
manager.resultValue = None

if type == "date":
console.error(tmp)
return datetime.datetime.fromtimestamp(math.floor(tmp/1000.0))

return tmp
else:
click.echo(title)
# click.echo(title) # not required
ret = click.prompt(text)
if type == "date":
def validate_date(date_text):
Expand All @@ -76,24 +96,24 @@ def validate_date(date_text):
except ValueError:
click.echo("Incorrect data format, should be YYYY-MM-DD")


return None

def validate_datetime(date_text):
try:
return datetime.datetime.fromisoformat(date_text)
except ValueError:
click.echo("Incorrect data format, should be YYYY-MM-DD HH:MM:SS")

return None

if use_time:
while not validate_date(ret):
ret = click.prompt(text)
else:
while not validate_datetime(ret):
ret = click.prompt(text)

return ret
elif type == "number":
def check_number(value: str):
Expand All @@ -120,19 +140,19 @@ def check_number(value: str):
return ret

async def input_date(*args, **kwargs):
kwargs.update({"type": "date"})
kwargs |= {"type": "date"}
return await input(*args, **kwargs)

async def input_number(*args, **kwargs):
kwargs.update({"type": "number"})
kwargs |= {"type": "number"}
return await input(*args, **kwargs)

async def input_string(*args, **kwargs):
kwargs.update({"type": "string"})
kwargs |= {"type": "string"}
return await input(*args, **kwargs)

async def input_text(*args, **kwargs):
kwargs.update({"type": "text"})
kwargs |= {"type": "text"}
return await input(*args, **kwargs)

input.date = input_date
Expand All @@ -151,7 +171,7 @@ async def select(title: str, text: str, choices: list[int], multiple: bool = Fal
_choices = to_js(_choices)

_self.postMessage(type="select", title=title, text=text, choices=_choices, multiple=multiple)
await wait();
await wait()

tmp = manager.resultValue
if multiple:
Expand All @@ -161,13 +181,13 @@ async def select(title: str, text: str, choices: list[int], multiple: bool = Fal

return tmp
else:
click.echo(title)
# click.echo(title) # not required
text += "\n\n"
for i, _ in enumerate(choices):
text += str(choices[i]) + (", " if i != len(choices)-1 else "")
if i > 0 and i % 3 == 0:
text += "\n"

lower_choices = [e.lower() for e in choices]
result_value = -1
while True:
Expand Down Expand Up @@ -204,8 +224,6 @@ async def select(title: str, text: str, choices: list[int], multiple: bool = Fal

return result_value



return None


2 changes: 1 addition & 1 deletion src/assets/scriptor/progressbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
if is_pyodide_context():
from js import self as _self

def start(total: int, step: int = -1, max_step=-1, txt = ''):
def start(total: int, step: int = -1, max_step: int = -1, txt = ""):
if is_pyodide_context():
_self.postMessage(type="progressbar", total=total, step=step, max_step=max_step, txt=txt)

Expand Down
2 changes: 1 addition & 1 deletion src/components/Interaction/Alert.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<sl-card class="interaction">
<div slot="header">
{{ t('alert') }}
{{ t('Alert') }}
</div>

<p class="paragraph">
Expand Down

0 comments on commit 4ff3156

Please sign in to comment.