在PyScript应用程序中,我在index.html
中提供了两个单选按钮,允许用户决定各向同性高斯blob的合成是否应该重叠(有效地通过切换make_blobs
方法的Scikit-Learn
的cluster_std
值)。
应用参考:https://24ec0d6b-0b55-49be-aeb7-a0046c41abf4.pyscriptapps.com/ea775901-75b9-406d-beac-944d26301b09/latest/
我目前无法将选择解析为Python布尔值(True
或False
),并且无论单击哪个单选按钮,都始终接收True
值。
尝试将HTML
中的值定义为双引号、单引号或未加引号,并在不同的大小写(标题、上部、下部)之间切换,但没有成功。也看了其他例子/问题在这里SO,E.例如How to pass a boolean from javascript to python?
HTML中相关部分的代码段:
<div id="input" style="margin: 20px;">
Should the clusters overlap: <br/>
<input py-click="generate_blobs()" type="radio" id="true" name="overlaps" value="true">
<label for="true"> True</label>
<input py-click="generate_blobs()" type="radio" id="false" name="overlaps" value="false">
<label for="false"> False</label>
</div>
对于Python代码段也是如此:
def generate_blobs():
"""Generate isotropic Gaussian blobs for clustering"""
over_laps = js.document.getElementsByName("overlaps")
for element in over_laps:
if element.checked:
overlap = bool(element.value)
print(overlap, type(overlap))
break
paragraph = Element("Overlap")
paragraph.write(f"overlaps: {overlap}")
n_samples = 4_000 # if overlap else 2_000
cluster_std = 1.5 if overlap else .4
X, y = datasets.make_blobs(
n_samples = n_samples,
n_features = 2,
centers = 5,
cluster_std = cluster_std,
shuffle = True,
)
return X, y
设置“假”选项的结果:
任何建议都是最受欢迎的,因为我用完的想法尝试
1条答案
按热度按时间4bbkushb1#
您的问题是由使用
py-click
引起的。py-click
无法从Javascript传递click-event。考虑对
js.document.querySelectorAll
的结果使用addEventListener
,这样您将获得可用于获取值的事件不要忘记将函数 Package 在
create_proxy
中,以确保在需要时将其删除。小例子: