-
Notifications
You must be signed in to change notification settings - Fork 29
/
colab_utils.py
70 lines (55 loc) · 1.77 KB
/
colab_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import warnings
def restart_runtime():
warnings.warn('Restarting runtime..')
if is_colab():
import os
os.kill(os.getpid(), 9)
else:
raise EnvironmentError('Restarting environment necessary..!')
def is_colab():
import sys
# noinspection PyBroadException
try:
# noinspection PyPackageRequirements
import google.colab
cimport = True
except:
cimport = False
if ('google.colab' in sys.modules) and cimport:
return True
else:
return False
def is_jupyter() -> bool:
"""It checks whether a module is run from a Jupyter notebook"""
try:
# noinspection PyUnresolvedReferences,PyStatementEffect
get_ipython
return True
except NameError:
# noinspection PyTypeChecker
return
# noinspection PyPep8Naming
class display:
def __init__(self):
self.display = None
def start_display(self, x=1200, y=2200):
# noinspection PyUnresolvedReferences,PyPackageRequirements
from pyvirtualdisplay import Display
self.display = Display(visible=0, size=(x, y))
self.display.start()
def stop_display(self):
self.display.stop()
# noinspection PyUnresolvedReferences,PyPackageRequirements
def showscreen(driver):
from IPython.display import display as imagerender
from IPython.display import Image
driver.save_screenshot('screen.png')
imagerender(Image('screen.png'))
def show_html(driver):
# noinspection PyUnresolvedReferences,PyPackageRequirements
from IPython.display import display, HTML, IFrame
display(HTML(str(driver.page_source)))
def exec_js(js: str):
# noinspection PyUnresolvedReferences,PyPackageRequirements
from IPython.display import Javascript
Javascript(js)