Esempio n. 1
0
                })

doc = HTMLDocument()
doc.set_title('Zepto Game | Sports forecasting research')
doc.add_header('Zepto Game | Sports forecasting research',
               level='h1',
               align='center')

local = pytz.timezone('Europe/Moscow')
time_now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
naive_time = datetime.datetime.strptime(f"{time_now}", "%Y-%m-%d %H:%M:%S")
local_dt_now = local.localize(naive_time, is_dst=None)
utc_dt_now = local_dt_now.astimezone(pytz.utc)

if len(today_champs) > 0:
    doc.add_text(f'Generated {str(utc_dt_now)}', size='14px')
    doc.add_line_break()
    doc.add_header('FOOTBALL', level='h3', align='center')
    for champ_ in today_champs:
        doc.add_header(f'{champ_}', level='h3')
        for pred_ in today_predictions:
            if pred_['champ_full_name'] == champ_:
                doc.add_table(pred_['df'])
                doc.add_line_break()
else:
    for _ in range(5):
        doc.add_line_break()
    doc.add_header('No predictions for today. Try it tomorrow.',
                   level='h2',
                   align='center')
    for _ in range(5):
Esempio n. 2
0
class HTML_Report:
    def __init__(self, name, title=None):
        self.name = name
        self.document = HTMLDocument()
        self.document.set_title(title if title is not None else name)
        self.footer = ''
        self.document.style = ( \
"""

body {
    max-width: 1060px;
    margin: auto;
    padding-bottom: 20px;
    font-family: "Lato", sans-serif;
}
p{
    font-family: sans-serif;
    overflow-x: hidden;
    color: var(--gray90);
    font-family: "Lato", sans-serif;
    font-size: 1.5rem;
    line-height: 1.5rem; 
    font-size:16px;
    text-indent: 0;
    text-align: left
}
div.pandas-dataframe {
    overflow: auto;
}
table.dataframe {
    border-collapse: collapse;
    border: none;
}
table.dataframe td {
  background-color: #fffef4;
  font-size: 14px;
  text-align: center;
  white-space: nowrap;
  margin: 0;
  padding-top: 0.4em;
  padding-bottom: 0.4em;
  padding-left: 0.5em;
  padding-right: 0.5em;
  border: 1px solid #d7d7d7;
}
table.dataframe th {
  background-color: #f9f9f9;
  font-size: 12px;
  text-align: center;
  white-space: nowrap;
  padding-left: 1em;
  padding-right: 1em;
  padding-top: 0.5em;
  padding-bottom: 0.5em;
  border: 1px solid #e0e0e0;
}
table.dataframe tr:nth-child(even) td {
  background: #fff2e4;
}
table.dataframe tr:nth-child(odd) td {
  background: #ffffe8;
}
table.dataframe tr:hover td {
  background-color: #ddeeff;
}
table.dataframe tbody th {
  font-weight: normal;
}

h4{
  background-color: #F8F7F7
}
h3{
  background-color: #FDF2E9
}
h2{
  background-color: #FAE5D3 
}
h1{
  background-color: #FDF2E9
}
""")

    def add_header(self, header, level: int = 1):
        self.document.add_header(header, level=f'h{level}', align='center')
        print('##' + '>>' * (5 - level) + ' ' + header + ' ' +
              (5 - level) * '<<' + '##')

    def add_text(self, text):
        self.document.add_text(
            text)  # defaults: size='16px', indent='0' alight='left'

    def add_html(self, text):
        self.document.body += text

    def add_line_break(self):
        self.document.add_line_break()

    def add_fig(self, fig, close=True):
        self.document.body += mpld3.fig_to_html(fig)
        if close:
            plt.close(fig)

    def add_df(self, df):
        self.document.add_table(df)

    def add_dict(self, dic):
        self.add_df(pd.Series(dic).to_frame().T)

    def add_img(self, image_url, title='', width_percent=100):
        self.document.add_image_link(image_url,
                                     title=title,
                                     width=f'{width_percent}%')

    def save(self, open=True):
        htmlfile = ProjectPaths.html_dir / f'{self.name} {uuid.uuid1()}.html'
        self.document.add_text(self.footer)
        self.document.write(htmlfile)
        print(f'{htmlfile} has been saved successfully!')
        if open:
            webbrowser.open_new_tab(htmlfile)

    def __exit__(self, exc_type, exc_val, exc_tb):
        try:
            self.save()
        except Exception as e:
            print(f'Can\'t save html. {e})')

    def __enter__(self):
        return self

    @staticmethod
    def __test__():
        with HTML_Report('test') as doc:
            doc.add_header("Test report")
            doc.add_text('this is text  ' * 20)
            doc.add_line_break()

            import numpy as np, datetime

            ts = pd.Series(np.random.randn(1000))
            ts = ts.cumsum()
            plot = ts.plot()
            fig = plot.get_figure()
            doc.add_fig(fig)

            arrays = [
                np.array(
                    ['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux']),
                np.array(
                    ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'])
            ]
            df = pd.DataFrame(np.random.randn(8, 4), index=arrays)
            doc.add_df(df)

            timestr = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            doc.add_text(f'Created at {timestr}')

    def add_footer(self, text):
        self.footer += text
Esempio n. 3
0
# Set document title
document.set_title('my first document title')

# Add main header
document.add_header('my first document header level 1',
                    level='h1',
                    align='center')

# Add section header
document.add_header(
    'section header level 2')  # defaults: level='h2' align='left'

# Add text paragraphs
document.add_text(', '.join(['this is text'] * 50),
                  size='14px',
                  indent='15px',
                  align='justify')
document.add_text(
    'another text')  # defaults: size='16px', indent='0' alight='left'

# Embed images
document.add_header('images section')
for i in range(0, 50):
    # get dummy image
    image_array = np.random.randint(0, 256, size=(4, 4, 3), dtype=np.uint8)
    if i > 47:
        # Enforce new line
        document.add_line_break()
    # Add image
    document.add_image(image=image_array,
                       title=f'image{i}',
Esempio n. 4
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_page_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_image(image=_get_image_array(),
                       title='title',
                       height=320,
                       width=480,
                       pixelated=False)

    document.add_image(image=_get_PIL_Image())

    _create_test_image()

    document.add_image(image=_TEST_IMAGE_PATH)

    document.add_image_link(image_link=_TEST_IMAGE_PATH,
                            title='title',
                            width='100%')

    document.add_image(image=pathlib.Path(_TEST_IMAGE_PATH))

    document.add_image_link(image_link=pathlib.Path(_TEST_IMAGE_PATH))

    _remove_test_image()

    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.write(_TEST_DOCUMENT_PATH)

    assert os.path.exists(_TEST_DOCUMENT_PATH)

    _remove_test_document()