import plotly.express as px
from htmlcreator import HTMLDocument

# Create new document with default CSS style
document = HTMLDocument()

# Set document title
document.set_title('my document with plotly figures')

# Embed plotly figure
document.add_header('plotly figure #1')
df = px.data.iris()
fig = px.scatter(df, x=df.sepal_length, y=df.sepal_width, color=df.species, size=df.petal_length)
fig.update_layout(title={'text': 'Iris Data Set', 'x': 0.5, 'xanchor': 'center'})
document.add_plotly_figure(fig)
# The first `add_plotly_figure` call includes `plotly.js` library in the document
# Including `plotly.js` makes HTML document self-contained but also heavier (+3 MB)

# Embed another plotly figure
document.add_header('plotly figure #2')
df = px.data.gapminder().query('continent=="Oceania"')
fig = px.line(df, x='year', y='lifeExp', color='country',
              color_discrete_sequence=px.colors.qualitative.Plotly[3:])
fig.update_layout(title={'text': 'Life expectancy: Oceania', 'x': 0.5, 'xanchor': 'center'})
document.add_plotly_figure(fig)

# Embed third plotly figure
document.add_header('plotly figure #3')
df = px.data.tips()
df['dayIndex'] = df['day'].map({'Thur': 4, 'Fri': 5, 'Sat': 6, 'Sun': 7})
df = df.sort_values('dayIndex')
Esempio n. 2
0
def test_HTMLDocument():
    document = HTMLDocument()

    assert document.style != ''

    document.set_title(title='title')

    document.add_header(header='header', level='h2', align='left')

    document.add_text(text='text', size='15px', indent='0', align='left')

    document.add_line_break()

    document.add_table(df=_get_df())

    try:
        document.add_table(df=[_get_df()])
    except Exception as e:
        assert isinstance(e, TypeError)
        assert str(
            e
        ) == "df is of type <class 'list'>, but it should be of type <class 'pandas.core.frame.DataFrame'>."

    document.add_page_break()

    document.add_image(image=_get_image_array(),
                       title='image from array',
                       height=320,
                       width=480,
                       pixelated=False)

    document.add_image(image=_get_PIL_Image(), title='image from PIL')

    _create_test_image()

    document.add_image(image=_TEST_IMAGE_PATH, title='image from filepath')

    document.add_image(image=pathlib.Path(_TEST_IMAGE_PATH),
                       title='image from Path')

    document.add_image_link(image_link=_TEST_IMAGE_PATH,
                            title='image link from filepath',
                            width='50%')

    document.add_image_link(image_link=pathlib.Path(_TEST_IMAGE_PATH),
                            title='image link from Path')

    try:
        document.add_image(image=_get_image_array().astype(np.float32))
    except Exception as e:
        assert isinstance(e, RuntimeError)
        assert str(e) == 'image.dtype is float32, but it should be uint8.'

    try:
        document.add_image(
            image=np.random.randint(0, 256, size=(1, 200, 200,
                                                  3)).astype(np.uint8))
    except Exception as e:
        assert isinstance(e, RuntimeError)
        assert str(e) == 'image.ndim is 4, but it should be 2 or 3.'

    try:
        document.add_image(image=[_get_image_array()])
    except Exception as e:
        assert isinstance(e, TypeError)
        assert str(
            e
        ) == "image is of type <class 'list'>, but it should be one of: <class 'numpy.ndarray'>, <class 'PIL.Image.Image'>, <class 'pathlib.Path'> or <class 'str'>."

    try:
        document.add_image_link(image_link=_get_image_array())
    except Exception as e:
        assert isinstance(e, TypeError)
        assert str(
            e
        ) == "image_link is of type <class 'numpy.ndarray'>, but it should be <class 'pathlib.Path'> or <class 'str'>."

    document.add_plotly_figure(fig=_get_plotly_figure(),
                               include_plotlyjs=False)

    document.add_plotly_figure(fig=_get_plotly_figure(), include_plotlyjs=True)

    try:
        document.add_plotly_figure(fig=_get_image_array())
    except Exception as e:
        assert isinstance(e, TypeError)
        assert str(
            e
        ) == "fig is of type <class 'numpy.ndarray'>, but it should be <class 'plotly.graph_objs._figure.Figure'>."

    document.write(_TEST_DOCUMENT_PATH)

    assert os.path.exists(_TEST_DOCUMENT_PATH)

    _remove_test_document()

    _remove_test_image()