<commit_before>from .html_writer import HTMLWriter
from matplotlib.animation import Animation
import matplotlib.pyplot as plt
import tempfile

def anim_to_html(anim, fps=None, embed_frames=True, default_mode='loop'):
    if fps is None and hasattr(anim, '_interval'):
        # Convert interval in ms to frames per second
        fps = 1000. / anim._interval

    plt.close(anim._fig)
    if hasattr(anim, "_html_representation"):
        return anim._html_representation
    else:
        with tempfile.NamedTemporaryFile(suffix='.html') as f:
            anim.save(f.name, writer=HTMLWriter(fps=fps,
                                                embed_frames=embed_frames,
                                                default_mode=default_mode))
            html = open(f.name).read()
        anim._html_representation = html
        return html


def display_animation(anim, **kwargs):
    from IPython.display import HTML
    return HTML(anim_to_html(anim, **kwargs))


Animation._repr_html_ = anim_to_html
<commit_msg>fix tempfile bug on windows<commit_after>from .html_writer import HTMLWriter
from matplotlib.animation import Animation
import matplotlib.pyplot as plt
import tempfile

def anim_to_html(anim, fps=None, embed_frames=True, default_mode='loop'):
    if fps is None and hasattr(anim, '_interval'):
        # Convert interval in ms to frames per second
        fps = 1000. / anim._interval

    plt.close(anim._fig)
    if hasattr(anim, "_html_representation"):
        return anim._html_representation
    else:
        # tempfile can't be used here: we need a filename, and this
        # fails on windows

        #with tempfile.NamedTemporaryFile(suffix='.html') as f:
        #    anim.save(f.name, writer=HTMLWriter(fps=fps,
        #                                        embed_frames=embed_frames,
        #                                        default_mode=default_mode))
        #    html = open(f.name).read()

        filename = './tmp_output_fdsahkrkdkskrt.html'  # poor-man's temp file
        anim.save(filename,  writer=HTMLWriter(fps=fps,
                                               embed_frames=embed_frames,
                                               default_mode=default_mode))
        html = open(filename).read()
        anim._html_representation = html
        return html


def display_animation(anim, **kwargs):
    from IPython.display import HTML
    return HTML(anim_to_html(anim, **kwargs))


Animation._repr_html_ = anim_to_html

{'fraction_non_alphanumeric': 0.08386837881219904, 'fraction_numerical': 0.0032102728731942215, 'mean_word_length': 2.2673656618610747, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6789799', 'n_tokens_mistral': 697, 'n_tokens_neox': 673, 'n_words': 169}