Example #1
0
    def test_support_binary_files_request(self):
        """Test support for binary files reads."""
        def _open_read(m, payload):
            is_binary = False
            args, kwargs = m.call_args
            if len(args) > 1:
                if "b" in args[1]:
                    is_binary = True
            encoding = "utf-8"
            if "encoding" in kwargs:
                encoding = kwargs["encoding"]

            if is_binary:
                from io import BytesIO

                return BytesIO(payload)
            else:
                from io import TextIOWrapper

                return TextIOWrapper(str(payload, encoding=encoding))

        with mock.patch("streamlit.components.v1.components.os.path.isdir"):
            declare_component("test", path=PATH)

        payload = b"\x00\x01\x00\x00\x00\x0D\x00\x80"  # binary non utf-8 payload

        with mock.patch("streamlit.components.v1.components.open") as m:
            m.return_value.__enter__ = lambda _: _open_read(m, payload)
            response = self._request_component("components_test.test")

        self.assertEqual(200, response.code)
        self.assertEqual(
            payload,
            response.body,
        )
Example #2
0
    def test_invalid_content_request(self):
        """Test request failure when invalid content (file) is provided."""

        with mock.patch("streamlit.components.v1.components.os.path.isdir"):
            declare_component("test", path=PATH)

        with mock.patch("streamlit.components.v1.components.open") as m:
            m.side_effect = OSError("Invalid content")
            response = self._request_component("components_test.test")

        self.assertEqual(404, response.code)
        self.assertEqual(
            b"components_test.test read error: Invalid content", response.body,
        )
Example #3
0
    def test_success_request(self):
        """Test request success when valid parameters are provided."""

        with mock.patch("streamlit.components.v1.components.os.path.isdir"):
            # We don't need the return value in this case.
            declare_component("test", path=PATH)

        with mock.patch(
                "streamlit.components.v1.components.open",
                mock.mock_open(read_data="Test Content"),
        ):
            response = self._request_component("components_test.test")

        self.assertEqual(200, response.code)
        self.assertEqual(b"Test Content", response.body)
Example #4
0
    def test_invalid_encoding_request(self):
        """Test request failure when invalid encoded file is provided."""

        with mock.patch("streamlit.components.v1.components.os.path.isdir"):
            declare_component("test", path=PATH)

        with mock.patch("streamlit.components.v1.components.open") as m:
            m.side_effect = UnicodeDecodeError("utf-8", b"", 9, 11,
                                               "unexpected end of data")
            response = self._request_component("components_test.test")

        self.assertEqual(404, response.code)
        self.assertEqual(
            b"components_test.test read error: 'utf-8' codec can't decode bytes in position 9-10: unexpected end of data",
            response.body,
        )
Example #5
0
data = init()

# _component_func = components.declare_component(
#         # We give the component a simple, descriptive name ("my_component"
#         # does not fit this bill, so please choose something better for your
#         # own component :)
#         "my_component",
#         # Pass `url` here to tell Streamlit that the component will be served
#         # by the local dev server that you run via `npm run start`.
#         # (This is useful while your component is in development.)
#         url="http://localhost:3001",
#     )

parent_dir = os.path.dirname(os.path.abspath(__file__))
build_dir = os.path.join(parent_dir, "component-build")
_component_func = components.declare_component("my_component", path=build_dir)


def my_component(name, key=None):
    """Create a new instance of "my_component".

    Parameters
    ----------
    name: str
        The name of the thing we're saying hello to. The component will display
        the text "Hello, {name}!"
    key: str or None
        An optional key that uniquely identifies this component. If this is
        None, and the component's arguments are changed, the component will
        be re-mounted in the Streamlit frontend and lose its current state.