Exemple #1
0
def instances():
    """
    Prints all urls to the current pieces of data being viewed
    """
    if global_state.size() > 0:

        def _instance_msgs():
            for data_id in global_state.keys():
                startup_url, final_app_root = build_startup_url_and_app_root()
                instance = DtaleData(
                    data_id,
                    startup_url,
                    is_proxy=JUPYTER_SERVER_PROXY,
                    app_root=final_app_root,
                )
                name = global_state.get_name(data_id)
                yield [data_id, name or "", instance.build_main_url()]
                if name is not None:
                    yield [
                        global_state.convert_name_to_url_path(name),
                        name,
                        instance.build_main_url(),
                    ]

        data = pd.DataFrame(list(_instance_msgs()),
                            columns=["ID", "Name",
                                     "URL"]).to_string(index=False)
        print((
            "To gain access to an instance object simply pass the value from 'ID' to dtale.get_instance(ID)\n\n{}"
        ).format(data))
    else:
        print("currently no running instances...")
Exemple #2
0
def get_store_contents():
    """
    Return an ordered tuple of attributes representing the store contents.
    Useful for ensuring key properties stay the same when switching between systems.
    """
    _get_one = [
        serialized_dataframe(global_state.get_data("1")),
        global_state.get_dtypes("1"),
        global_state.get_settings("1"),
        global_state.get_metadata("1"),
        global_state.get_context_variables("1"),
        global_state.get_history("1"),
    ]
    _get_all = [
        {
            int(k): serialized_dataframe(v.data)
            for k, v in global_state.items()
        },
        {int(k): v.dtypes
         for k, v in global_state.items()},
        {int(k): v.settings
         for k, v in global_state.items()},
        {int(k): v.metadata
         for k, v in global_state.items()},
        {int(k): v.context_variables
         for k, v in global_state.items()},
        {int(k): v.history
         for k, v in global_state.items()},
    ]
    _lengths = [
        global_state.size(),
    ]
    return (_get_one, _get_all, _lengths)
Exemple #3
0
def instances():
    """
    Prints all urls to the current pieces of data being viewed
    """
    if global_state.size() > 0:

        def _instance_msgs():
            for data_id in global_state.keys():
                data_obj = DtaleData(data_id, build_url(ACTIVE_PORT, ACTIVE_HOST))
                name = global_state.get_name(data_id)
                yield [data_id, name or "", data_obj.build_main_url(data_id=data_id)]
                if name is not None:
                    yield [
                        global_state.convert_name_to_url_path(name),
                        name,
                        data_obj.build_main_url(),
                    ]

        data = pd.DataFrame(
            list(_instance_msgs()), columns=["ID", "Name", "URL"]
        ).to_string(index=False)
        print(
            (
                "To gain access to an instance object simply pass the value from 'ID' to dtale.get_instance(ID)\n\n{}"
            ).format(data)
        )
    else:
        print("currently no running instances...")
Exemple #4
0
def get_data_id(pathname):
    """
    Parses data ID from query path (ex: 'foo/bar/1' => '1')
    """
    data_id = pathname.split("/")[-1]
    if (global_state.size()) == 0:
        return None
    if global_state.contains(data_id):
        return data_id
    else:
        return sorted(global_state.keys())[0]
Exemple #5
0
def test_show(unittest):
    from dtale.app import show, get_instance, instances
    import dtale.views as views
    import dtale.global_state as global_state

    global_state.clear_store()
    instances()
    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,
                        ignore_duplicate=True)
        assert "http://localhost:9999" == instance._url
        assert "http://localhost:9999/dtale/main/1" == instance.main_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(
            global_state.get_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
        # removed data name test. wait for proper data name implementation.

        instances()

        assert get_instance(
            20) is None  # should return None for invalid data ids

        instance.kill()
        mock_requests.assert_called_once()
        assert mock_requests.call_args[0][
            0] == "http://localhost:9999/shutdown"

        instance3 = show(data=test_data,
                         subprocess=False,
                         name="It's Here",
                         ignore_duplicate=True)
        assert instance3.main_url() == "http://localhost:9999/dtale/main/2"
        pdt.assert_frame_equal(instance3.data, test_data)

    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,
            ignore_duplicate=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

    # RangeIndex test
    test_data = pd.DataFrame([1, 2, 3])
    with ExitStack() as stack:
        stack.enter_context(mock.patch("dtale.app.DtaleFlask", MockDtaleFlask))
        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",
                        ignore_duplicate=True)
        assert np.array_equal(instance.data["0"].values, test_data[0].values)

    with ExitStack() as stack:
        stack.enter_context(mock.patch("dtale.app.DtaleFlask", MockDtaleFlask))
        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()))
        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="foo1",
            notebook=True,
            ignore_duplicate=True,
        )
        mock_display.assert_called_once()
        mock_iframe.assert_called_once()
        assert mock_iframe.call_args[0][
            0] == "http://localhost:9999/dtale/iframe/{}".format(
                instance._data_id)

        assert type(instance.__str__()).__name__ == "str"
        assert type(instance.__repr__()).__name__ == "str"

    class MockDtaleFlaskRunTest(Flask):
        def __init__(self,
                     import_name,
                     reaper_on=True,
                     url=None,
                     app_root=None,
                     *args,
                     **kwargs):
            kwargs.pop("instance_relative_config", None)
            kwargs.pop("static_url_path", None)
            super(MockDtaleFlaskRunTest,
                  self).__init__(import_name, *args, **kwargs)

        def run(self, *args, **kwargs):
            assert self.jinja_env.auto_reload
            assert self.config["TEMPLATES_AUTO_RELOAD"]

    with mock.patch("dtale.app.DtaleFlask", MockDtaleFlaskRunTest):
        show(
            data=test_data,
            subprocess=False,
            port=9999,
            debug=True,
            ignore_duplicate=True,
        )

    with mock.patch("dtale.app._thread.start_new_thread",
                    mock.Mock()) as mock_thread:
        show(data=test_data, subprocess=True, ignore_duplicate=True)
        mock_thread.assert_called()

    test_data = pd.DataFrame([dict(a=1, b=2)])

    with ExitStack() as stack:
        mock_build_app = stack.enter_context(
            mock.patch("dtale.app.build_app", 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("requests.get", mock.Mock()))
        show(data=test_data,
             subprocess=False,
             name="foo2",
             ignore_duplicate=True)

        _, kwargs = mock_build_app.call_args
        unittest.assertEqual(
            {
                "app_root": None,
                "host": "localhost",
                "reaper_on": True
            },
            kwargs,
            "build_app should be called with defaults",
        )

    # test adding duplicate column
    with ExitStack() as stack:
        stack.enter_context(mock.patch("dtale.app.DtaleFlask", MockDtaleFlask))
        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("requests.get", mock.Mock()))
        instance = show(
            data=pd.DataFrame([dict(a=1, b=2)]),
            subprocess=False,
            name="foo3",
            ignore_duplicate=True,
        )
        with pytest.raises(Exception):
            instance.data = instance.data.rename(columns={"b": "a"})

        curr_instance_ct = global_state.size()
        show(data=pd.DataFrame([dict(a=1, b=2)]), subprocess=False, name="foo")
        assert curr_instance_ct == global_state.size()

    # cleanup
    global_state.cleanup()
Exemple #6
0
def test_upload(unittest):
    import dtale.views as views
    import dtale.global_state as global_state

    global_state.clear_store()
    df, _ = views.format_data(pd.DataFrame([1, 2, 3]))
    with build_app(url=URL).test_client() as c:
        build_data_inst({c.port: df})
        global_state.set_dtypes(c.port, views.build_dtypes_state(df))

        resp = c.post("/dtale/upload")
        assert not resp.get_json()["success"]

        c.post(
            "/dtale/upload",
            data={
                "tests_df.csv": (build_upload_data(), "test_df.csv"),
                "separatorType": "csv",
            },
        )
        assert global_state.size() == 2
        new_key = next((k for k in global_state.keys() if k != c.port), None)
        assert list(global_state.get_data(new_key).columns) == ["a", "b", "c"]

    with build_app(url=URL).test_client() as c:
        global_state.clear_store()
        build_data_inst({c.port: df})
        global_state.set_dtypes(c.port, views.build_dtypes_state(df))

        resp = c.post("/dtale/upload")
        assert not resp.get_json()["success"]

        c.post(
            "/dtale/upload",
            data={
                "tests_df.csv": (build_upload_data(), "test_df.csv"),
                "separatorType": "custom",
                "separator": ",",
            },
        )
        assert global_state.size() == 2
        new_key = next((k for k in global_state.keys() if k != c.port), None)
        assert list(global_state.get_data(new_key).columns) == ["a", "b", "c"]

    with build_app(url=URL).test_client() as c:
        global_state.clear_store()
        build_data_inst({c.port: df})
        global_state.set_dtypes(c.port, views.build_dtypes_state(df))
        assert global_state.size() == 1
        if PY3:
            c.post(
                "/dtale/upload",
                data={
                    "test_df.xlsx": (
                        os.path.join(
                            os.path.dirname(__file__), "..", "data/test_df.xlsx"
                        ),
                        "test_df.xlsx",
                    )
                },
            )
            assert global_state.size() == 2
            new_key = next((k for k in global_state.keys() if k != c.port), None)
            assert list(global_state.get_data(new_key).columns) == ["a", "b", "c"]

    with build_app(url=URL).test_client() as c:
        with ExitStack() as stack:
            global_state.clear_store()
            data = {c.port: df}
            build_data_inst(data)
            global_state.set_dtypes(c.port, views.build_dtypes_state(df))
            stack.enter_context(
                mock.patch(
                    "dtale.views.pd.read_excel",
                    mock.Mock(
                        return_value={
                            "Sheet 1": pd.DataFrame(dict(a=[1], b=[2])),
                            "Sheet 2": pd.DataFrame(dict(c=[1], d=[2])),
                        }
                    ),
                )
            )
            resp = c.post(
                "/dtale/upload",
                data={
                    "test_df.xlsx": (
                        os.path.join(
                            os.path.dirname(__file__), "..", "data/test_df.xlsx"
                        ),
                        "test_df.xlsx",
                    )
                },
            )
            assert global_state.size() == 3
            sheets = resp.json["sheets"]
            assert len(sheets) == 2
            unittest.assertEqual(
                sorted([s["name"] for s in sheets]),
                ["Sheet 1", "Sheet 2"],
            )
Exemple #7
0
def test_web_upload(unittest):
    import dtale.global_state as global_state

    global_state.clear_store()
    with build_app(url=URL).test_client() as c:
        with ExitStack() as stack:
            load_csv = stack.enter_context(
                mock.patch(
                    "dtale.cli.loaders.csv_loader.loader_func",
                    mock.Mock(return_value=pd.DataFrame(dict(a=[1], b=[2]))),
                )
            )
            load_excel = stack.enter_context(
                mock.patch(
                    "dtale.cli.loaders.excel_loader.load_file",
                    mock.Mock(
                        return_value={"Sheet 1": pd.DataFrame(dict(a=[1], b=[2]))}
                    ),
                )
            )
            load_json = stack.enter_context(
                mock.patch(
                    "dtale.cli.loaders.json_loader.loader_func",
                    mock.Mock(return_value=pd.DataFrame(dict(a=[1], b=[2]))),
                )
            )
            params = {"type": "csv", "url": "http://test.com"}
            c.get("/dtale/web-upload", query_string=params)
            load_csv.assert_called_once()
            unittest.assertEqual(
                load_csv.call_args.kwargs,
                {"path": "http://test.com", "proxy": None},
            )
            assert global_state.size() == 1
            load_csv.reset_mock()

            params = {"type": "tsv", "url": "http://test.com"}
            c.get("/dtale/web-upload", query_string=params)
            load_csv.assert_called_once()
            unittest.assertEqual(
                load_csv.call_args.kwargs,
                {"path": "http://test.com", "proxy": None, "delimiter": "\t"},
            )
            assert global_state.size() == 2

            params = {
                "type": "json",
                "url": "http://test.com",
                "proxy": "http://testproxy.com",
            }
            c.get("/dtale/web-upload", query_string=params)
            load_json.assert_called_once()
            unittest.assertEqual(
                load_json.call_args.kwargs,
                {"path": "http://test.com", "proxy": "http://testproxy.com"},
            )
            assert global_state.size() == 3

            params = {"type": "excel", "url": "http://test.com"}
            c.get("/dtale/web-upload", query_string=params)
            load_excel.assert_called_once()
            unittest.assertEqual(
                load_excel.call_args.kwargs,
                {"path": "http://test.com", "proxy": None},
            )
            assert global_state.size() == 4
            global_state.clear_store()
            load_excel.reset_mock()
            load_excel.return_value = {
                "Sheet 1": pd.DataFrame(dict(a=[1], b=[2])),
                "Sheet 2": pd.DataFrame(dict(c=[1], d=[2])),
            }
            resp = c.get("/dtale/web-upload", query_string=params)
            sheets = resp.json["sheets"]
            assert len(sheets) == 2
            unittest.assertEqual(
                sorted([s["name"] for s in sheets]),
                ["Sheet 1", "Sheet 2"],
            )