Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
instance2 = get_instance(instance._data_id)
assert instance2._url == instance._url
assert instances()[instance._data_id]._url == instance._url
instance.kill()
mock_requests.assert_called_once()
mock_requests.call_args[0][0] == 'http://localhost:9999/shutdown'
assert views.METADATA['1']['name'] == 'foo'
with ExitStack() as stack:
mock_run = stack.enter_context(mock.patch('dtale.app.DtaleFlask.run', mock.Mock()))
mock_find_free_port = stack.enter_context(mock.patch('dtale.app.find_free_port', mock.Mock(return_value=9999)))
stack.enter_context(mock.patch('socket.gethostname', mock.Mock(return_value='localhost')))
stack.enter_context(mock.patch('dtale.app.is_up', mock.Mock(return_value=False)))
mock_data_loader = mock.Mock(return_value=test_data)
instance = show(data_loader=mock_data_loader, subprocess=False, port=9999, force=True, debug=True)
assert 'http://localhost:9999' == instance._url
mock_run.assert_called_once()
mock_find_free_port.assert_not_called()
mock_data_loader.assert_called_once()
_, kwargs = mock_run.call_args
assert '9999' in instance._url
with ExitStack() as stack:
mock_run = stack.enter_context(mock.patch('dtale.app.DtaleFlask.run', mock.Mock()))
stack.enter_context(mock.patch('dtale.app.find_free_port', mock.Mock(return_value=9999)))
stack.enter_context(mock.patch('socket.gethostname', mock.Mock(return_value='localhost')))
stack.enter_context(mock.patch('dtale.app.is_up', mock.Mock(return_value=True)))
mock_data_loader = mock.Mock(return_value=test_data)
mock_webbrowser = stack.enter_context(mock.patch('webbrowser.get'))
instance = show(data_loader=mock_data_loader, subprocess=False, port=9999, open_browser=True)
def test_show(unittest, builtin_pkg):
from dtale.app import show, get_instance, instances
import dtale.views as views
test_data = pd.DataFrame([dict(a=1, b=2)])
with ExitStack() as stack:
mock_run = stack.enter_context(mock.patch('dtale.app.DtaleFlask.run', mock.Mock()))
mock_find_free_port = stack.enter_context(mock.patch('dtale.app.find_free_port', mock.Mock(return_value=9999)))
stack.enter_context(mock.patch('socket.gethostname', mock.Mock(return_value='localhost')))
stack.enter_context(mock.patch('dtale.app.is_up', mock.Mock(return_value=False)))
mock_requests = stack.enter_context(mock.patch('requests.get', mock.Mock()))
instance = show(data=test_data, subprocess=False, name='foo')
assert 'http://localhost:9999' == instance._url
mock_run.assert_called_once()
mock_find_free_port.assert_called_once()
pdt.assert_frame_equal(instance.data, test_data)
tmp = test_data.copy()
tmp['biz'] = 2.5
instance.data = tmp
unittest.assertEqual(
views.DTYPES[instance._data_id],
views.build_dtypes_state(tmp),
'should update app data/dtypes'
)
instance2 = get_instance(instance._data_id)
assert instance2._url == instance._url
assert 'http://localhost:9999' == instance._url
mock_run.assert_called_once()
mock_find_free_port.assert_not_called()
mock_data_loader.assert_called_once()
_, kwargs = mock_run.call_args
assert '9999' in instance._url
with ExitStack() as stack:
mock_run = stack.enter_context(mock.patch('dtale.app.DtaleFlask.run', mock.Mock()))
stack.enter_context(mock.patch('dtale.app.find_free_port', mock.Mock(return_value=9999)))
stack.enter_context(mock.patch('socket.gethostname', mock.Mock(return_value='localhost')))
stack.enter_context(mock.patch('dtale.app.is_up', mock.Mock(return_value=True)))
mock_data_loader = mock.Mock(return_value=test_data)
mock_webbrowser = stack.enter_context(mock.patch('webbrowser.get'))
instance = show(data_loader=mock_data_loader, subprocess=False, port=9999, open_browser=True)
mock_run.assert_not_called()
webbrowser_instance = mock_webbrowser.return_value
assert 'http://localhost:9999/dtale/main/3' == webbrowser_instance.open.call_args[0][0]
instance.open_browser()
assert 'http://localhost:9999/dtale/main/3' == webbrowser_instance.open.mock_calls[1][1][0]
# RangeIndex test
test_data = pd.DataFrame([1, 2, 3])
with ExitStack() as stack:
stack.enter_context(mock.patch('dtale.app.DtaleFlask.run', mock.Mock()))
stack.enter_context(mock.patch('dtale.app.find_free_port', mock.Mock(return_value=9999)))
stack.enter_context(mock.patch('socket.gethostname', mock.Mock(return_value='localhost')))
stack.enter_context(mock.patch('dtale.app.is_up', mock.Mock(return_value=False)))
stack.enter_context(mock.patch('dtale.app.logger', mock.Mock()))
instance = show(data=test_data, subprocess=False, name='foo')
assert np.array_equal(instance.data['0'].values, test_data[0].values)
stack.enter_context(mock.patch('dtale.app.find_free_port', mock.Mock(return_value=9999)))
stack.enter_context(mock.patch('socket.gethostname', mock.Mock(return_value='localhost')))
stack.enter_context(mock.patch('dtale.app.logger', mock.Mock()))
stack.enter_context(mock.patch('dtale.views.in_ipython_frontend', mock.Mock(return_value=True)))
get_calls = {'ct': 0}
getter = namedtuple('get', 'ok')
def mock_requests_get(url, verify=True):
if url.endswith('/health'):
is_ok = get_calls['ct'] > 0
get_calls['ct'] += 1
return getter(is_ok)
return getter(True)
stack.enter_context(mock.patch('requests.get', mock_requests_get))
instance = show(data=test_data, subprocess=False, name='foo')
assert instance.__repr__() == ''
assert instance.__str__() == ''
instance.adjust_cell_dimensions(height=600)
# cleanup
views.DATA = {}
views.DTYPES = {}
views.SETTINGS = {}
views.METADATA = {}
mock_display.assert_called_once()
mock_iframe.assert_called_once()
assert mock_iframe.call_args[0][0] == 'http://localhost:9999/dtale/iframe/5'
assert type(instance.__str__()).__name__ == 'str'
assert type(instance.__repr__()).__name__ == 'str'
def mock_run(self, *args, **kwargs):
assert self.jinja_env.auto_reload
assert self.config['TEMPLATES_AUTO_RELOAD']
with mock.patch('dtale.app.DtaleFlask.run', mock_run):
show(data=test_data, subprocess=False, port=9999, debug=True)
with mock.patch('dtale.app._thread.start_new_thread', mock.Mock()) as mock_thread:
show(data=test_data, subprocess=True)
mock_thread.assert_called()
# cleanup
views.DATA = {}
views.DTYPES = {}
views.SETTINGS = {}
views.METADATA = {}
stack.enter_context(mock.patch('dtale.app.logger', mock.Mock()))
stack.enter_context(mock.patch('dtale.views.in_ipython_frontend', mock.Mock(return_value=False)))
get_calls = {'ct': 0}
getter = namedtuple('get', 'ok')
def mock_requests_get(url, verify=True):
if url.endswith('/health'):
is_ok = get_calls['ct'] > 0
get_calls['ct'] += 1
return getter(is_ok)
return getter(True)
stack.enter_context(mock.patch('requests.get', mock_requests_get))
mock_display = stack.enter_context(mock.patch('IPython.display.display', mock.Mock()))
mock_iframe = stack.enter_context(mock.patch('IPython.display.IFrame', mock.Mock()))
instance = show(data=test_data, subprocess=True, name='foo', notebook=True)
mock_display.assert_called_once()
mock_iframe.assert_called_once()
assert mock_iframe.call_args[0][0] == 'http://localhost:9999/dtale/iframe/5'
assert type(instance.__str__()).__name__ == 'str'
assert type(instance.__repr__()).__name__ == 'str'
def mock_run(self, *args, **kwargs):
assert self.jinja_env.auto_reload
assert self.config['TEMPLATES_AUTO_RELOAD']
with mock.patch('dtale.app.DtaleFlask.run', mock_run):
show(data=test_data, subprocess=False, port=9999, debug=True)
with mock.patch('dtale.app._thread.start_new_thread', mock.Mock()) as mock_thread:
show(data=test_data, subprocess=True)
mock_display = stack.enter_context(mock.patch('IPython.display.display', mock.Mock()))
mock_iframe = stack.enter_context(mock.patch('IPython.display.IFrame', mock.Mock()))
instance = show(data=test_data, subprocess=True, name='foo', notebook=True)
mock_display.assert_called_once()
mock_iframe.assert_called_once()
assert mock_iframe.call_args[0][0] == 'http://localhost:9999/dtale/iframe/5'
assert type(instance.__str__()).__name__ == 'str'
assert type(instance.__repr__()).__name__ == 'str'
def mock_run(self, *args, **kwargs):
assert self.jinja_env.auto_reload
assert self.config['TEMPLATES_AUTO_RELOAD']
with mock.patch('dtale.app.DtaleFlask.run', mock_run):
show(data=test_data, subprocess=False, port=9999, debug=True)
with mock.patch('dtale.app._thread.start_new_thread', mock.Mock()) as mock_thread:
show(data=test_data, subprocess=True)
mock_thread.assert_called()
# cleanup
views.DATA = {}
views.DTYPES = {}
views.SETTINGS = {}
views.METADATA = {}
instance = show(data_loader=mock_data_loader, subprocess=False, port=9999, open_browser=True)
mock_run.assert_not_called()
webbrowser_instance = mock_webbrowser.return_value
assert 'http://localhost:9999/dtale/main/3' == webbrowser_instance.open.call_args[0][0]
instance.open_browser()
assert 'http://localhost:9999/dtale/main/3' == webbrowser_instance.open.mock_calls[1][1][0]
# RangeIndex test
test_data = pd.DataFrame([1, 2, 3])
with ExitStack() as stack:
stack.enter_context(mock.patch('dtale.app.DtaleFlask.run', mock.Mock()))
stack.enter_context(mock.patch('dtale.app.find_free_port', mock.Mock(return_value=9999)))
stack.enter_context(mock.patch('socket.gethostname', mock.Mock(return_value='localhost')))
stack.enter_context(mock.patch('dtale.app.is_up', mock.Mock(return_value=False)))
stack.enter_context(mock.patch('dtale.app.logger', mock.Mock()))
instance = show(data=test_data, subprocess=False, name='foo')
assert np.array_equal(instance.data['0'].values, test_data[0].values)
orig_import = __import__
mock_swaggu = mock.Mock()
def import_mock(name, *args, **kwargs):
if name == 'flasgger':
return mock_swaggu
return orig_import(name, *args, **kwargs)
with ExitStack() as stack:
stack.enter_context(mock.patch('{}.__import__'.format(builtin_pkg), side_effect=import_mock))
stack.enter_context(mock.patch('dtale.app.DtaleFlask.run', mock.Mock()))
stack.enter_context(mock.patch('dtale.app.find_free_port', mock.Mock(return_value=9999)))
stack.enter_context(mock.patch('socket.gethostname', mock.Mock(return_value='localhost')))
def main(host=None, port=None, debug=False, no_reaper=False, open_browser=False, name=None, **kwargs):
"""
Runs a local server for the D-Tale application.
This local server is recommended when you have a pandas object stored in a CSV
or retrievable from :class:`arctic.Arctic` data store.
"""
log_opts = get_log_options(kwargs)
setup_logging(log_opts.get('logfile'), log_opts.get('log_level'), log_opts.get('verbose'))
data_loader = check_loaders(kwargs)
show(host=host, port=int(port or find_free_port()), debug=debug, subprocess=False, data_loader=data_loader,
reaper_on=not no_reaper, open_browser=open_browser, name=name, **kwargs)