<commit_before>from ipywidgets import interact, FloatSlider, Text, Dropdown, Button, fixed
import pickle, os
from rtpipe import interactive
from IPython.display import display


def save(obj, label):
    """ Save or update obj as pkl file with name label """

    # initialize hidden state directory
    if not os.path.exists('.nbpipeline'): os.mkdir('.nbpipeline')

    # read obj, if it exists
    objloc = '.nbpipeline/{0}'.format(label)
    if os.path.exists(objloc):
        with open(objloc, 'r') as pkl:
            objorig = pickle.load(pkl)

        # if obj is different from saved obj, then update saved obj
        if objorig != obj:
            with open(objloc, 'w') as pkl:
                pickle.dump(obj, pkl)

    # if obj file not there, initialize it
    else:
        with open(objloc, 'w') as pkl:
            pickle.dump(obj, pkl)


def read(label):
    """ Read obj with give label from hidden state directory """

    objloc = '.nbpipeline/{0}'.format(label)
    if os.path.exists(objloc):
        obj = pickle.load(open(objloc, 'r')) 
    else:
        obj = None

    return obj


def list():
    """ List names of stored objects """

    print(os.listdir('.nbpipeline/'))


def setText(label, default='', description='Set Text'):
    """ Set text in a notebook pipeline (via interaction or with nbconvert) """

    obj = read(label)
    if obj == None:
        obj=default
        save(obj, label)  # initialize with default

    textw = Text(value=obj, description=description)
    hndl = interact(save, obj=textw, label=fixed(label))


def setFloat(label, default=0, min=-20, max=20, description='Set Float'):
    """ Set float in a notebook pipeline (via interaction or with nbconvert) """

    obj = read(label)
    if obj == None:
        obj=default
        save(obj, label)  # initialize with default

    floatw = FloatSlider(value=obj, min=min, max=max, description=description)
    hndl = interact(save, obj=floatw, label=fixed(label))


def setDropdown(label, default=None, options=[], description='Set Dropdown'):
    """ Set float in a notebook pipeline (via interaction or with nbconvert) """

    obj = read(label)
    if obj == None:
        obj=default
        save(obj, label)  # initialize with default

    dropdownw = Dropdown(value=obj, options=options, description=description)
    hndl = interact(save, obj=dropdownw, label=fixed(label))


def setButton(function, description=''):
    """ Create button for clicking to run function """

    button = Button(description=description, value=False)
    display(button)
    button.on_click(function)<commit_msg>nbname function added. can parse pkl or txt can set statedir<commit_after>from ipywidgets import interact, FloatSlider, Text, Dropdown, Button, fixed
import pickle, os
from IPython.display import display, Javascript


def save(obj, label, format='text', statedir='.nbpipeline'):
    """ Save or update obj as pkl file with name label 

    format can be 'text' or 'pickle'.
    statedir is the directory name for saving object
    """

    # initialize hidden state directory
    if not os.path.exists(statedir): os.mkdir(statedir)

    objloc = '{0}/{1}'.format(statedir, label)

    with open(objloc, 'w') as fp:
        if format == 'pickle':
            pickle.dump(obj, fp)
        elif format == 'text':
            fp.write(str(obj))


def read(label, statedir='.nbpipeline'):
    """ Read obj with give label from hidden state directory """

    objloc = '{0}/{1}'.format(statedir, label)

    try:
        obj = pickle.load(open(objloc, 'r')) 
    except (KeyError, IndexError):
        obj = open(objloc, 'r').read()
        try:
            obj = float(obj)
        except ValueError:
            pass
    except IOError:
        obj = None

    return obj


def list(statedir = '.nbpipeline'):
    """ List names of stored objects """

    print(os.listdir(statedir))


def setText(label, default='', description='Set Text', format='text', statedir='.nbpipeline'):
    """ Set text in a notebook pipeline (via interaction or with nbconvert) """

    obj = read(label)
    if obj == None:
        obj=default
        save(obj, label)  # initialize with default

    textw = Text(value=obj, description=description)
    hndl = interact(save, obj=textw, label=fixed(label), format=fixed(format), statedir=fixed(statedir))


def setFloat(label, default=0, min=-20, max=20, description='Set Float', format='text', statedir='.nbpipeline'):
    """ Set float in a notebook pipeline (via interaction or with nbconvert) """

    obj = read(label)
    if obj == None:
        obj=default
        save(obj, label)  # initialize with default

    floatw = FloatSlider(value=obj, min=min, max=max, description=description)
    hndl = interact(save, obj=floatw, label=fixed(label), format=fixed(format), statedir=fixed(statedir))


def setDropdown(label, default=None, options=[], description='Set Dropdown', format='pickle', statedir='.nbpipeline'):
    """ Set float in a notebook pipeline (via interaction or with nbconvert) """

    obj = read(label)
    if obj == None:
        obj=default
        save(obj, label)  # initialize with default

    dropdownw = Dropdown(value=obj, options=options, description=description)
    hndl = interact(save, obj=dropdownw, label=fixed(label), format=fixed(format), statedir=fixed(statedir))


def setButton(function, description=''):
    """ Create button for clicking to run function """

    def function2(b):
        function()

    button = Button(description=description, value=False)
    display(button)
    button.on_click(function2)

def setnbname():
    """ Runs javascript to get name of notebook. Saved as python obj 'nbname' """
    display(Javascript("""IPython.notebook.kernel.execute("nbname = " + "\'"+IPython.notebook.notebook_name+"\'");"""))
{'fraction_non_alphanumeric': 0.12719449225473323, 'fraction_numerical': 0.003098106712564544, 'mean_word_length': 3.3626126126126126, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3177795', 'n_tokens_mistral': 1755, 'n_tokens_neox': 1697, 'n_words': 527}