-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy path__init__.py
60 lines (50 loc) · 1.55 KB
/
__init__.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
import atexit
import json
import os
import webbrowser
from pathlib import Path
from urllib.request import HTTPError, Request, urlopen
from . import randomize as Randomize
from .commander import Commander
from .layouts import *
from .tracers import *
from .types import PathLike
__all__ = (
"Randomize", "Commander",
"Array1DTracer", "Array2DTracer", "ChartTracer", "GraphTracer", "LogTracer", "Tracer",
"HorizontalLayout", "Layout", "VerticalLayout"
)
def create_json_file(path: PathLike = "./visualization.json"):
commands = json.dumps(Commander.commands, separators=(",", ":"))
with Path(path).open("w", encoding="UTF-8") as file:
file.write(commands)
def get_url() -> str:
url = "https://algorithm-visualizer.org/api/visualizations"
commands = json.dumps(Commander.commands, separators=(",", ":")).encode('utf-8')
request = Request(
url,
method="POST",
data=commands,
headers={
"Content-type": "application/json; charset=utf-8",
"Content-Length": len(commands)
}
)
response = urlopen(request)
if response.status == 200:
return response.read().decode('utf-8')
else:
raise HTTPError(
url=url,
code=response.status,
msg="Failed to retrieve the scratch URL: non-200 response",
hdrs=dict(response.info()),
fp=None
)
@atexit.register
def execute():
if os.getenv("ALGORITHM_VISUALIZER"):
create_json_file()
else:
url = get_url()
webbrowser.open(url)