示例#1
0
def _get_posters_html(movielens_posters_df: pd.DataFrame,
                      df_item: pd.DataFrame,
                      item_ids: Iterable[Any],
                      col_description: str = 'Recommended films:',
                      titles_provided: bool = False,
                      image_width: Optional[int] = 500) -> str:
    if type(item_ids) == int:
        item_ids = [item_ids]

    final = pd.Series(dtype='object')
    if not titles_provided:
        try:
            top_movies_titles = [df_item[df_item['item_id'] == x]
                                 ['movie_title'].iloc[0] for x in item_ids]
        except IndexError as e:
            raise IndexError('Ensure user and item IDs start at 1 for MovieLens 100K data:', e)
    else:
        top_movies_titles = item_ids
    for x in item_ids:
        url = _get_single_poster_html(movielens_posters_df, x)
        final = final.append(pd.Series(url))

    final_df = final.to_frame()

    final_df.index = top_movies_titles
    final_df.columns = [col_description]

    return df_to_html(final_df,
                      image_cols=[col_description],
                      transpose=True,
                      image_width=image_width)
示例#2
0
def _get_posters_html(movielens_posters_df: pd.DataFrame,
                      df_item: pd.DataFrame,
                      item_ids: Union[int, Iterable[Any]],
                      col_description: str = 'Recommended films:',
                      image_width: Optional[int] = 500) -> str:
    if not isinstance(item_ids, collections.abc.Iterable):
        item_ids = [item_ids]

    top_movies_titles = [
        df_item[df_item['item_id'] == x]['movie_title'].iloc[0]
        for x in item_ids
    ]
    final_urls = []

    for item_id in item_ids:
        url = ''
        with suppress((ValueError, TypeError)):
            url = movielens_posters_df.query(
                f'item_id == {item_id}')['url'].item()

        final_urls.append(url)

    final_df = pd.DataFrame(final_urls)

    final_df.index = top_movies_titles
    final_df.columns = [col_description]

    return df_to_html(df=final_df,
                      image_cols=[col_description],
                      transpose=True,
                      image_width=image_width)
示例#3
0
def test_df_to_html(df_html_test):
    expected = """
        <table border="1" class="dataframe">
            <thead>
            <tr style="text-align: right;">

                <th></th>
                <th>title</th>
                <th>description</th>
                <th>link</th>

                <th>image</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <th>0</th>

                <td><h2>Greg</h2></td>
                <td>some text here</td>

                <td><a target="_blank" href="https://madeupsite.com">
                    https://madeupsite.com</a></td>
                <td><img src="https://avatars0.githubusercontent.co
                        m/u/13399445"></td>
            </tr>
            <tr>
                <th>1</th>
                <td><h2>Real Greg</h2>
                </td>
                <td>more text here</td>
                <td><a target="_blank" href="https://anotherm
                        adeupsite.com">https://anothermadeupsite.com</a></td>

                <td><img src="https://avatars3.githubusercontent.com/u/31417712"></td>
            </tr>

            </tbody>
        </table>
        """
    actual = df_to_html(
        df_html_test,
        image_cols='image',
        hyperlink_cols=['link', 'image'],
        html_tags={'title': 'h2', 'image': 'em'},
        max_num_rows=None,
    )

    assert expected.replace('\n', '').replace('  ', '') == (
        actual.replace('\n', '').replace('  ', '')
    )
示例#4
0
def test_df_to_html_with_nonexistent_cols(df_html_test):
    with pytest.raises(ValueError):
        df_to_html(df_html_test, image_cols='nonexistent_col')

    with pytest.raises(ValueError):
        df_to_html(df_html_test, hyperlink_cols='nonexistent_col')

    with pytest.raises(ValueError):
        df_to_html(df_html_test, html_tags={'nonexistent_col': 'mark'})