[Python] Display HTML class not showing inside an if-else block in Jupyter Notebook

Discussão em 'Python' iniciado por Stack, Maio 7, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using this class to represent the dataframes in a .ipynb file:

    # Handy class for DataFrame viz
    class display(object):
    '''
    Display HTML representation of multiple objects
    '''

    template = '''<div style="float: left; padding: 10px;">
    <p style='font-family:"Courier New", Courier, monospace'>{0}</p>{1}
    </div>'''

    def __init__(self, *args):
    self.args = args

    def _repr_html_(self):
    return '\n'.join(self.template.format(a, eval(a)._repr_html_()) for a in self.args)

    def __repr__(self):
    return '\n\n'.join(a + '\n' + repr(eval(a)) for a in self.args)


    This class is not working inside an if-else block and I don't understand why.

    Can anyone give an explanation? Thx in advance!


    Chunk to replicate the issue:

    import pandas as pd

    # Handy class for DataFrame viz

    # Create a sample DataFrame with 3 rows and 5 columns
    df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6],
    'C': [7, 8, 9],
    'D': [10, 11, 12],
    'E': [13, 14, 15]
    })

    # Display the DataFrame
    display('df')

    >>>

    df

    A B C D E
    0 1 4 7 10 13
    1 2 5 8 11 14
    2 3 6 9 12 15


    inside = True

    if inside:
    display('df')

    >>>

    Continue reading...

Compartilhe esta Página