-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
86 lines (65 loc) · 2.35 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import asyncio
import os.path
import pkgutil
from collections import OrderedDict
import sys
import misc.plane
from pywebio.output import *
from pywebio.platform.tornado import start_server
from pywebio.session import *
def get_animation_modules():
pkgpath = os.path.dirname(__file__)
module_names = [m.name for m in pkgutil.walk_packages([pkgpath]) if not m.ispkg]
module_names.sort()
modules = OrderedDict() # name -> module
for m in module_names:
try:
__import__(m)
except:
continue
module = sys.modules[m]
if hasattr(module, 'frames'):
modules[m] = module
return modules
modules = get_animation_modules()
async def main():
"""ASCII Animations
Animations created entirely out of text.
"""
set_env(output_animation=False)
module_name = (await eval_js("window.location.hash") or '')[1:]
print(module_name)
current_module = modules.get(module_name) or misc.plane
changed = False
def change_anim(module_name):
nonlocal current_module, changed
if module_name not in modules:
return
current_module = modules[module_name]
changed = True
with use_scope('title', clear=True):
put_markdown('### %s' % current_module.name)
run_js('window.location.hash="#%s"' % module_name)
put_markdown("""## ASCII Animations
Animations created entirely out of text. [Learn more about ASCII Animations](https://www.incredibleart.org/links/ascii.html).
When viewing on cell phones, it is recommended to turn to landscape mode.
The content is collected from the Internet. [Source code on Github](https://github.com/wang0618/ascii-art).
""", strip_indent=4)
put_buttons([
(getattr(m, 'name', name), name)
for name, m in modules.items()
], onclick=change_anim)
with use_scope('title'):
put_markdown('### %s' % current_module.name)
set_scope('code')
while 1:
duration = getattr(current_module, 'duration', 100)
for i in current_module.frames:
with use_scope('code', clear=True):
put_code(i.replace("\n\r", "\n"))
await asyncio.sleep(duration / 1000.0)
if changed:
changed = False
break
if __name__ == '__main__':
start_server(main, port=8080)