Write (and Run) Python in Blog
2022-05-05
Trying out PyScript, a new tool that embeds (runnable) Python code inside HTML. Cool huh? Even better, it’s developed and maintained by Anaconda team which makes it sound more promising and trustworthy, at least in the long run.
Charts above are randomly generated when this page loads, by scripts below. Specificaly, what’s shown on the left is OLS fitting on a Pareto/Normal mixture model, and on the right the residual distribution.
<div id="plot"></div>
<py-script output="plot">
import numpy as np
from types import SimpleNamespace
import scipy.stats as ss
import statsmodels.api as sm
import matplotlib.pyplot as plt
n = 1000
u = ss.pareto.rvs(1.5, size=n)
v = ss.norm.rvs(0, 1, size=n)
x = np.random.randn(n)
y = x * 12.5 + .25 + u + v
reg = sm.OLS(y, sm.add_constant(x.reshape(-1, 1))).fit()
x_fit = np.linspace(x.min(), x.max(), 10)
y_fit = sm.add_constant(x_fit.reshape(-1, 1)) @ reg.params
colors = dict(black=(0, 0, 0), red=(187, 34, 34), blue=(51, 102, 153))
for c, v in colors.items(): colors[c] = tuple(i / 255 for i in v)
colors = SimpleNamespace(**colors)
fig, (ax1, ax2) = plt.subplots(figsize=(8, 4), ncols=2)
ax1.scatter(x, y, s=1, c=[colors.black])
ax1.plot(x_fit, y_fit, c=colors.red)
ax2.hist(reg.resid, fc=colors.blue, density=True, bins=30, ec=colors.black)
plt.tight_layout()
fig
</py-script>
As you may have noticed, I’m taking an extra effort to define colors as RGB tuples in my custom namespace, and the charts lack anything textual - axis labels, titles, legends. This is because whenever I try to use single or double quotation marks, there comes a SyntaxError
that prevents the code from proceeding. Also, it seems to be the case that codes inside <py-script>
tags do not support empty lines, which is kinda important (and maybe not so). Unfortunately as PyScript is still an experimental repo, I haven’t seen anyone posting about these on their issues page. Maybe I should be the one.