human
I am working on the file "00 Javascript In Notebooks.ipynb".
The first task is:
Generating Javascript from Python
You often want to generate Javascript from Python.  
Here's a hack that takes a fragment of Javascript, finds all the functions that are declared, and generates bound Python methods for each function:
Can you write Python program for it?
assistant
#Here is the Python program you requested.

import types
from functools import partial
import json

class JSCode(object):
    def __init__(self, code):
        self.code = code
        self.invokes = ""
        
        # Find all the function name(  lines in the Javasript code
        for line in code.split("\n"):
            line = line.strip().split()
            if line and line[0] == 'function':
                funcname = line[1].split("(")[0]
                
                # For each create a bound method to add to the list of invocations
                def invoke(self, *args):
                    argstr = ','.join([json.dumps(arg) for arg in args])
                    self.invokes += "%s(%s);\n" % (funcname, argstr)                

                setattr(self, funcname, types.MethodType(invoke, self))    
                
    def _repr_javascript_(self):
        """Return the code definitions and all invocations"""
        return self.code + self.invokes