import base64 import json import numpy as np from panel.pane import DataFrame, JSON, HTML, Markdown, PaneBase, Pane, Str from panel.tests.util import pd_available, streamz_available def test_get_markdown_pane_type(): assert PaneBase.get_pane_type("**Markdown**") is Markdown @pd_available def test_get_dataframe_pane_type(): import pandas as pd df = pd._testing.makeDataFrame() assert PaneBase.get_pane_type(df) is DataFrame @pd_available def test_get_series_pane_type(): import pandas as pd df = pd._testing.makeDataFrame() assert PaneBase.get_pane_type(df.iloc[:, 0]) is DataFrame @streamz_available def test_get_streamz_dataframe_pane_type(): from streamz.dataframe import Random sdf = Random(interval='200ms', freq='50ms') assert PaneBase.get_pane_type(sdf) is DataFrame @streamz_available def test_get_streamz_dataframes_pane_type(): from streamz.dataframe import Random sdf = Random(interval='200ms', freq='50ms').groupby('y').sum() assert PaneBase.get_pane_type(sdf) is DataFrame @streamz_available def test_get_streamz_series_pane_type(): from streamz.dataframe import Random sdf = Random(interval='200ms', freq='50ms') assert PaneBase.get_pane_type(sdf.x) is DataFrame @streamz_available def test_get_streamz_seriess_pane_type(): from streamz.dataframe import Random sdf = Random(interval='200ms', freq='50ms').groupby('y').sum() assert PaneBase.get_pane_type(sdf.x) is DataFrame def test_markdown_pane(document, comm): pane = Pane("**Markdown**") # Create pane model = pane.get_root(document, comm=comm) assert pane._models[model.ref['id']][0] is model assert model.text.endswith("<p><strong>Markdown</strong></p>") # Replace Pane.object pane.object = "*Markdown*" assert pane._models[model.ref['id']][0] is model assert model.text.endswith("<p><em>Markdown</em></p>") # Cleanup pane._cleanup(model) assert pane._models == {} def test_markdown_pane_dedent(document, comm): pane = Pane(" ABC") # Create pane model = pane.get_root(document, comm=comm) assert pane._models[model.ref['id']][0] is model assert model.text.endswith("<p>ABC</p>") pane.dedent = False assert model.text.startswith('<div class="codehilite') def test_markdown_pane_extensions(document, comm): pane = Pane(""" ```python None ``` """) # Create pane model = pane.get_root(document, comm=comm) assert pane._models[model.ref['id']][0] is model assert model.text.startswith('<div class="codehilite') pane.extensions = ["extra", "smarty"] assert model.text.startswith('<pre><code class="language-python') def test_html_pane(document, comm): pane = HTML("

Test

") # Create pane model = pane.get_root(document, comm=comm) assert pane._models[model.ref['id']][0] is model assert model.text == "<h1>Test</h1>" # Replace Pane.object pane.object = "

Test

" assert pane._models[model.ref['id']][0] is model assert model.text == "<h2>Test</h2>" # Cleanup pane._cleanup(model) assert pane._models == {} @pd_available def test_dataframe_pane_pandas(document, comm): import pandas as pd pane = DataFrame(pd._testing.makeDataFrame()) # Create pane model = pane.get_root(document, comm=comm) assert pane._models[model.ref['id']][0] is model assert model.text.startswith('<table') orig_text = model.text # Replace Pane.object pane.object = pd._testing.makeMixedDataFrame() assert pane._models[model.ref['id']][0] is model assert model.text.startswith('<table') assert model.text != orig_text # Cleanup pane._cleanup(model) assert pane._models == {} @pd_available def test_dataframe_pane_supports_escape(document, comm): import pandas as pd url = "Panel" df = pd.DataFrame({"url": [url]}) pane = DataFrame(df) # Create pane model = pane.get_root(document, comm=comm) assert pane._models[model.ref['id']][0] is model assert pane.escape assert "<a href='https://panel.holoviz.org/'>Panel</a>" not in model.text pane.escape = False assert "<a href='https://panel.holoviz.org/'>Panel</a>" in model.text # Cleanup pane._cleanup(model) assert pane._models == {} @streamz_available def test_dataframe_pane_streamz(document, comm): from streamz.dataframe import Random sdf = Random(interval='200ms', freq='50ms') pane = DataFrame(sdf) assert pane._stream is None # Create pane model = pane.get_root(document, comm=comm) assert pane._stream is not None assert pane._models[model.ref['id']][0] is model assert model.text == '' # Replace Pane.object pane.object = sdf.x assert pane._models[model.ref['id']][0] is model assert model.text == '' # Cleanup pane._cleanup(model) assert pane._stream is None assert pane._models == {} def test_string_pane(document, comm): pane = Str("

Test

") # Create pane model = pane.get_root(document, comm=comm) assert pane._models[model.ref['id']][0] is model assert model.text == "<pre><h1>Test</h1></pre>" # Replace Pane.object pane.object = "

Test

" assert pane._models[model.ref['id']][0] is model assert model.text == "<pre><h2>Test</h2></pre>" # Cleanup pane._cleanup(model) assert pane._models == {} class NumpyEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, np.ndarray): data_b64 = base64.b64encode(obj.data).decode('utf-8') return dict(__ndarray__=data_b64, dtype=str(obj.dtype), shape=obj.shape) return json.JSONEncoder.default(self, obj) def test_json_applies(): assert JSON.applies({1: 2}) assert JSON.applies([1, 2, 3]) assert JSON.applies('{"a": 1}') == 0 assert not JSON.applies({'array': np.array([1, 2, 3])}) assert JSON.applies({'array': np.array([1, 2, 3])}, encoder=NumpyEncoder) def test_json_pane(document, comm): pane = JSON({'a': 2}) model = pane.get_root(document, comm=comm) assert model.text == '{"a": 2}' assert pane._models[model.ref['id']][0] is model pane.object = '{"b": 3}' assert model.text == '{"b": 3}' assert pane._models[model.ref['id']][0] is model pane.object = {"test": "can't show this"} assert model.text == '{"test": "can\'t show this"}' assert pane._models[model.ref['id']][0] is model pane.object = ["can't show this"] assert model.text == '["can\'t show this"]' assert pane._models[model.ref['id']][0] is model pane.object = "can't show this" assert model.text == '"can\'t show this"' assert pane._models[model.ref['id']][0] is model pane.object = "can show this" assert model.text == '"can show this"' assert pane._models[model.ref['id']][0] is model # Cleanup pane._cleanup(model) assert pane._models == {} def test_json_pane_rerenders_on_depth_change(document, comm): pane = JSON({'a': 2}, depth=2) model = pane.get_root(document, comm=comm) pane.depth = -1 assert model.depth is None