Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4b90ebd

Browse files
Bring back pyweb as it was (pyscript#2105)
1 parent 15c19aa commit 4b90ebd

File tree

12 files changed

+1704
-71
lines changed

12 files changed

+1704
-71
lines changed

pyscript.core/package-lock.json

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

pyscript.core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pyscript/core",
3-
"version": "0.4.48",
3+
"version": "0.4.50",
44
"type": "module",
55
"description": "PyScript",
66
"module": "./index.js",

pyscript.core/src/stdlib/pyscript/event_handling.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,16 @@ def when(event_type=None, selector=None):
1919
"""
2020

2121
def decorator(func):
22-
23-
from pyscript.web.elements import Element, ElementCollection
24-
2522
if isinstance(selector, str):
2623
elements = document.querySelectorAll(selector)
2724
else:
2825
# TODO: This is a hack that will be removed when pyscript becomes a package
2926
# and we can better manage the imports without circular dependencies
30-
if isinstance(selector, Element):
27+
from pyweb import pydom
28+
29+
if isinstance(selector, pydom.Element):
3130
elements = [selector._js]
32-
elif isinstance(selector, ElementCollection):
31+
elif isinstance(selector, pydom.ElementCollection):
3332
elements = [el._js for el in selector]
3433
else:
3534
raise ValueError(
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .pydom import JSProperty
2+
from .pydom import dom as pydom
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
from pyodide.ffi import to_js
2+
from pyscript import window
3+
4+
5+
class Device:
6+
"""Device represents a media input or output device, such as a microphone,
7+
camera, or headset.
8+
"""
9+
10+
def __init__(self, device):
11+
self._js = device
12+
13+
@property
14+
def id(self):
15+
return self._js.deviceId
16+
17+
@property
18+
def group(self):
19+
return self._js.groupId
20+
21+
@property
22+
def kind(self):
23+
return self._js.kind
24+
25+
@property
26+
def label(self):
27+
return self._js.label
28+
29+
def __getitem__(self, key):
30+
return getattr(self, key)
31+
32+
@classmethod
33+
async def load(cls, audio=False, video=True):
34+
"""Load the device stream."""
35+
options = window.Object.new()
36+
options.audio = audio
37+
if isinstance(video, bool):
38+
options.video = video
39+
else:
40+
# TODO: Think this can be simplified but need to check it on the pyodide side
41+
42+
# TODO: this is pyodide specific. shouldn't be!
43+
options.video = window.Object.new()
44+
for k in video:
45+
setattr(
46+
options.video,
47+
k,
48+
to_js(video[k], dict_converter=window.Object.fromEntries),
49+
)
50+
51+
stream = await window.navigator.mediaDevices.getUserMedia(options)
52+
return stream
53+
54+
async def get_stream(self):
55+
key = self.kind.replace("input", "").replace("output", "")
56+
options = {key: {"deviceId": {"exact": self.id}}}
57+
58+
return await self.load(**options)
59+
60+
61+
async def list_devices() -> list[dict]:
62+
"""
63+
Return the list of the currently available media input and output devices,
64+
such as microphones, cameras, headsets, and so forth.
65+
66+
Output:
67+
68+
list(dict) - list of dictionaries representing the available media devices.
69+
Each dictionary has the following keys:
70+
* deviceId: a string that is an identifier for the represented device
71+
that is persisted across sessions. It is un-guessable by other
72+
applications and unique to the origin of the calling application.
73+
It is reset when the user clears cookies (for Private Browsing, a
74+
different identifier is used that is not persisted across sessions).
75+
76+
* groupId: a string that is a group identifier. Two devices have the same
77+
group identifier if they belong to the same physical device — for
78+
example a monitor with both a built-in camera and a microphone.
79+
80+
* kind: an enumerated value that is either "videoinput", "audioinput"
81+
or "audiooutput".
82+
83+
* label: a string describing this device (for example "External USB
84+
Webcam").
85+
86+
Note: the returned list will omit any devices that are blocked by the document
87+
Permission Policy: microphone, camera, speaker-selection (for output devices),
88+
and so on. Access to particular non-default devices is also gated by the
89+
Permissions API, and the list will omit devices for which the user has not
90+
granted explicit permission.
91+
"""
92+
# https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/enumerateDevices
93+
return [
94+
Device(obj) for obj in await window.navigator.mediaDevices.enumerateDevices()
95+
]

0 commit comments

Comments
 (0)