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

Skip to content

Commit fe74179

Browse files
authored
Lazy import av (#432)
1 parent 775556e commit fe74179

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

fastplotlib/layouts/_frame/_toolbar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from fastplotlib.layouts._subplot import Subplot
1+
from .._subplot import Subplot
22

33

44
class ToolBar:

fastplotlib/layouts/_record_mixin.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
from multiprocessing import Queue, Process
44
from time import time
55

6-
try:
7-
import av
8-
except ImportError:
9-
HAS_AV = False
10-
else:
11-
HAS_AV = True
6+
7+
def _get_av():
8+
try:
9+
import av
10+
except ImportError:
11+
raise ModuleNotFoundError(
12+
"Recording to video file requires `av`:\n"
13+
"https://github.com/PyAV-Org/PyAV"
14+
) from None
15+
else:
16+
return av
1217

1318

1419
class VideoWriterAV(Process):
@@ -28,6 +33,7 @@ def __init__(
2833
super().__init__()
2934
self.queue = queue
3035

36+
av = _get_av()
3137
self.container = av.open(path, mode="w")
3238

3339
self.stream = self.container.add_stream(codec, rate=fps, options=options)
@@ -45,6 +51,7 @@ def __init__(
4551
self.stream.pix_fmt = pixel_format
4652

4753
def run(self):
54+
av = _get_av()
4855
while True:
4956
if self.queue.empty(): # no frame to write
5057
continue
@@ -177,12 +184,6 @@ def record_start(
177184
178185
"""
179186

180-
if not HAS_AV:
181-
raise ModuleNotFoundError(
182-
"Recording to video file requires `av`:\n"
183-
"https://github.com/PyAV-Org/PyAV"
184-
)
185-
186187
if Path(path).exists():
187188
raise FileExistsError(f"File already exists at given path: {path}")
188189

0 commit comments

Comments
 (0)