Skip to content

Commit e999286

Browse files
committed
Up through antigravity example.
1 parent 48da4d9 commit e999286

20 files changed

+326
-145
lines changed

poetry.lock

Lines changed: 30 additions & 119 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ classifiers = [
1616
Changelog = "https://github.com/pauleveritt/psc/releases"
1717

1818
[tool.poetry.dependencies]
19-
python = "^3.7"
19+
python = "^3.10"
2020
click = ">=8.0.1"
2121
nox = "^2022.1.7"
2222
typer = { extras = ["all"], version = "^0.6.1" }
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>Altair Visualization</title>
5+
<link rel="icon" type="image/png" href="../../../favicon.png">
6+
<script defer src="../../../pyscript/pyscript.js"></script>
7+
</head>
8+
<body>
9+
<py-config src="../py_config.toml">
10+
packages=[
11+
"altair",
12+
"pandas",
13+
"vega_datasets"
14+
]
15+
</py-config>
16+
<div id="altair" style="width: 100%; height: 100%"></div>
17+
<py-script output="altair">
18+
import altair as alt
19+
from vega_datasets import data
20+
21+
source = data.movies.url
22+
23+
pts = alt.selection(type="single", encodings=['x'])
24+
25+
rect = alt.Chart(data.movies.url).mark_rect().encode(
26+
alt.X('IMDB_Rating:Q', bin=True),
27+
alt.Y('Rotten_Tomatoes_Rating:Q', bin=True),
28+
alt.Color('count()',
29+
scale=alt.Scale(scheme='greenblue'),
30+
legend=alt.Legend(title='Total Records')
31+
)
32+
)
33+
34+
circ = rect.mark_point().encode(
35+
alt.ColorValue('grey'),
36+
alt.Size('count()',
37+
legend=alt.Legend(title='Records in Selection')
38+
)
39+
).transform_filter(
40+
pts
41+
)
42+
43+
bar = alt.Chart(source).mark_bar().encode(
44+
x='Major_Genre:N',
45+
y='count()',
46+
color=alt.condition(pts, alt.ColorValue("steelblue"), alt.ColorValue("grey"))
47+
).properties(
48+
width=550,
49+
height=200
50+
).add_selection(pts)
51+
52+
alt.vconcat(
53+
rect + circ,
54+
bar
55+
).resolve_legend(
56+
color="independent",
57+
size="independent"
58+
)
59+
</py-script>
60+
</body>
61+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: Altair Visualization
3+
subtitle: Declarative statistical visualization library.
4+
---
5+
Visualizing the IMDB ranking.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""Animate the xkcd comic."""
2+
import random
3+
import sys
4+
5+
from js import DOMParser
6+
from js import document
7+
from js import setInterval
8+
from pyodide.ffi import create_proxy
9+
from pyodide.http import open_url
10+
11+
12+
class Antigravity:
13+
"""Implement antigravity."""
14+
15+
url = "./antigravity.svg"
16+
17+
def __init__(self, target=None, interval=10, append=True, fly=False):
18+
"""Construct antigravity."""
19+
target = target or sys.stdout._out
20+
self.target = (
21+
document.getElementById(target) if isinstance(target, str) else target
22+
)
23+
doc = DOMParser.new().parseFromString(
24+
open_url(self.url).read(), "image/svg+xml"
25+
)
26+
self.node = doc.documentElement
27+
if append:
28+
self.target.append(self.node)
29+
else:
30+
self.target.replaceChildren(self.node)
31+
self.xoffset, self.yoffset = 0, 0
32+
self.interval = interval
33+
if fly:
34+
self.fly()
35+
36+
def fly(self):
37+
"""Schedule a taking off-and-fly."""
38+
setInterval(create_proxy(self.move), self.interval)
39+
40+
def move(self):
41+
"""Move the item."""
42+
char = self.node.getElementsByTagName("g")[1]
43+
char.setAttribute("transform", f"translate({self.xoffset}, {-self.yoffset})")
44+
self.xoffset += random.normalvariate(0, 1) / 20
45+
if self.yoffset < 50:
46+
self.yoffset += 0.1
47+
else:
48+
self.yoffset += random.normalvariate(0, 1) / 20
49+
50+
51+
_auto = Antigravity(append=True)
52+
fly = _auto.fly

src/psc/gallery/examples/antigravity/antigravity.svg

Lines changed: 72 additions & 0 deletions
Loading
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>xkcd Antigravity</title>
5+
<link rel="icon" type="image/png" href="../../../favicon.png">
6+
<script defer src="../../../pyscript/pyscript.js"></script>
7+
</head>
8+
<body>
9+
<py-config src="../py_config.toml">
10+
paths = ["./antigravity.py"]
11+
</py-config>
12+
<strong>Based on xkcd: antigravity https://xkcd.com/353/.</strong>
13+
<py-script>
14+
import antigravity
15+
16+
antigravity.fly()
17+
</py-script>
18+
19+
</body>
20+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: xkcd Antigravity
3+
subtitle: We can fly!
4+
---
5+
Based on the [xkcd antigravity](https://xkcd.com/353/)

src/psc/gallery/examples/hello_world/index.html

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4-
<meta charset="utf-8">
5-
<meta name="viewport" content="width=device-width,initial-scale=1">
6-
74
<title>Hello World</title>
8-
95
<link rel="icon" type="image/png" href="../../../favicon.png">
106
<script defer src="../../../pyscript/pyscript.js"></script>
117
<link rel="stylesheet" href="hello_world.css">
12-
<script defer src="hello_world.js"></script>
138
</head>
149
<body>
1510
<py-config src="../py_config.toml"></py-config>

src/psc/gallery/examples/hello_world/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
title: Hello World
33
subtitle: The classic hello world, but in Python -- in a browser!
44
---
5-
The *body* description.
5+
The *body* description.

0 commit comments

Comments
 (0)