Ejemplo n.º 1
0
def run_py_code():
    remember_where = lisp.point()
    # check if the line contains \inputminted
    lisp.beginning_of_line()
    l1 = lisp.point()
    lisp.end_of_line()
    l2 = lisp.point()
    line = lisp.buffer_substring(l1,l2)
    # if code comes from file
    if "\\inputminted" in line:
        block_begin = lisp.point()
        lisp.message(line)
        py_file = re.search("\{python\}\{(.*?)\}", line).groups(1)[0]
        # get code content from file
        curr_dir = os.path.dirname(lisp.buffer_file_name())
        content = open(curr_dir + "/" + py_file).read()
        block_end = l2 # end of block happens to be end of include file line
        lisp.goto_char(remember_where)
    else:
        # get code content from latex
        block_begin,block_end,content = get_block_content("\\begin{minted}","\\end{minted}")
        
    # we have code content at this point

    # scan content to find plt.plot(). if there is, scan buffer
    # previous to *here* to determine order of _this_ plt.plot(), and
    # give it an appropiate index that will be appended to the end of
    # the .png image file, i.e. [buffer name]_[index].png. plt.plot()
    # commands will be replaced by the corresponding plt.savefig
    # command.

    # generate savefig for execution code (no output in emacs yet)
    bc = get_buffer_content_prev(block_begin)
    plt_count_before = len(re.findall('plt\.savefig\(',bc))
    base = os.path.splitext(lisp.buffer_name())[0]
    f = '%s_%s.png' % (base, two_digit(plt_count_before+1))
    rpl = "plt.hold(False)\nplt.savefig('%s')\nplt.hold(False)" % f
    show_replaced = True if "plt.show()" in content else False
    content=content.replace("plt.show()",rpl)
    include_graphics_command = "\\includegraphics[height=6cm]{%s}" % f

    #(ip) = get_kernel_pointer(lisp.buffer_name())
    start = time.time()
    
    with capture_output() as io:
        res_code = ip.run_cell(content)
    res = io.stdout

    elapsed = (time.time() - start)
    display_results(block_end, res) # display it

    # generate includegraphics command
    if show_replaced:
        lisp.forward_line(2) # skip over end verbatim, leave one line emtpy
        lisp.insert(include_graphics_command + '\n')
        lisp.scroll_up(1) # skip over end verbatim, leave one line emtpy        
        lisp.goto_char(remember_where)
        lisp.replace_string("plt.show()",rpl,None,block_begin,block_end)
        
    lisp.goto_char(remember_where)
    if "plt.savefig" in content: lisp.preview_buffer()
    
    lisp.message("Ran in " + str(elapsed) + " seconds")
Ejemplo n.º 2
0
def run_py_code():
    remember_where = lisp.point()
    # check if the line contains \inputminted
    lisp.beginning_of_line()
    l1 = lisp.point()
    lisp.end_of_line()
    l2 = lisp.point()
    line = lisp.buffer_substring(l1, l2)
    # if code comes from file
    if "\\inputminted" in line:
        block_begin = lisp.point()
        lisp.message(line)
        py_file = re.search("\{python\}\{(.*?)\}", line).groups(1)[0]
        # get code content from file
        curr_dir = os.path.dirname(lisp.buffer_file_name())
        content = open(curr_dir + "/" + py_file).read()
        block_end = l2  # end of block happens to be end of include file line
        lisp.goto_char(remember_where)
    else:
        # get code content from latex
        block_begin, block_end, content = get_block_content(
            "\\begin{minted}", "\\end{minted}")

    # we have code content at this point

    # scan content to find plt.plot(). if there is, scan buffer
    # previous to *here* to determine order of _this_ plt.plot(), and
    # give it an appropiate index that will be appended to the end of
    # the .png image file, i.e. [buffer name]_[index].png. plt.plot()
    # commands will be replaced by the corresponding plt.savefig
    # command.

    # generate savefig for execution code (no output in emacs yet)
    bc = lisp.buffer_string()
    plt_count_before = len(re.findall('plt\.savefig\(', bc))
    base = os.path.splitext(lisp.buffer_name())[0]
    f = '%s_%s.png' % (base, two_digit(plt_count_before + 1))
    rpl = "plt.savefig('%s')" % f
    show_replaced = True if "plt.show()" in content else False
    content = content.replace("plt.show()", rpl)
    content = "plt.figure()\n" + content
    include_graphics_command = "\\includegraphics[height=6cm]{%s}" % f

    #(ip) = get_kernel_pointer(lisp.buffer_name())
    start = time.time()

    with capture_output() as io:
        res_code = get_ip().run_cell(content)
    res = io.stdout

    elapsed = (time.time() - start)
    display_results(block_end, res)  # display it

    # generate includegraphics command
    if show_replaced:
        lisp.forward_line(2)  # skip over end verbatim, leave one line emtpy
        lisp.insert(include_graphics_command + '\n')
        lisp.scroll_up(1)  # skip over end verbatim, leave one line emtpy
        lisp.goto_char(remember_where)
        lisp.replace_string("plt.show()", rpl, None, block_begin, block_end)

    lisp.goto_char(remember_where)
    if "plt.savefig" in content: lisp.preview_buffer()

    lisp.message("Ran in " + str(elapsed) + " seconds")
Ejemplo n.º 3
0
def run_py_code():
    remember_where = lisp.point()
    # check if the line contains \inputminted
    lisp.beginning_of_line()
    l1 = lisp.point()
    lisp.end_of_line()
    l2 = lisp.point()
    line = lisp.buffer_substring(l1, l2)
    # if code comes from file
    if "\\inputminted" in line:
        lisp.message(line)
        py_file = re.search("\{python\}\{(.*?)\}", line).groups(1)[0]
        # get code content from file
        curr_dir = os.path.dirname(lisp.buffer_file_name())
        content = open(curr_dir + "/" + py_file).read()
        block_end = l2  # end of block happens to be end of include file line
        lisp.goto_char(remember_where)
    else:
        # get code content from latex
        block_begin, block_end, content = get_block_content(
            "\\begin{minted}", "\\end{minted}")

    #lisp.message(content)

    # we have code content at this point

    # scan content to find plt.plot(). if there is, scan buffer
    # previous to *here* to determine order of _this_ plt.plot(), and
    # give it an appropiate index that will be appended to the end of
    # the .png image file, i.e. [buffer name]_[index].png. plt.plot()
    # commands will be replaced by the corresponding plt.savefig
    # command.

    # generate savefig for execution code (no output in emacs yet)
    bc = get_buffer_content_prev(block_begin)
    plt_count_before = len(re.findall('plt\.savefig\(', bc))
    base = os.path.splitext(lisp.buffer_name())[0]
    f = '%s_%s.png' % (base, two_digit(plt_count_before + 1))
    rpl = "plt.savefig('%s')" % f
    show_replaced = True if "plt.show()" in content else False
    content = content.replace("plt.show()", rpl)
    include_graphics_command = "\\includegraphics[height=6cm]{%s}" % f

    (kc, kernel, ip) = get_kernel_pointer(lisp.buffer_name())
    start = time.time()
    res = ''
    with capture_output() as io:
        ip.run_cell(content)
    res = io.stdout
    if kernel.shell.last_known_outflag:
        etype, value, tb = kernel.shell._get_exc_info()
        res = str(etype) + " " + str(value) + "\n"
    elapsed = (time.time() - start)
    # replace this unnecessary message so output becomes blank
    if res and len(res) > 0:  # if result not empty
        res = res.replace(
            "Populating the interactive namespace from numpy and matplotlib\n",
            "")
        display_results(block_end, res)  # display it
    else:
        display_results(block_end, "")
        lisp.goto_char(block_end)

    # generate includegraphics command
    if show_replaced:
        lisp.forward_line(2)  # skip over end verbatim, leave one line emtpy
        lisp.insert(include_graphics_command + '\n')
        lisp.backward_line_nomark(
            1)  # skip over end verbatim, leave one line emtpy
        lisp.goto_char(remember_where)
        lisp.replace_string("plt.show()", rpl, None, block_begin, block_end)

    lisp.goto_char(remember_where)
    if "plt.savefig" in content: lisp.preview_buffer()

    lisp.message("Ran in " + str(elapsed) + " seconds")
Ejemplo n.º 4
0
def run_py_code():
    remember_where = lisp.point()
    # check if the line contains \inputminted
    lisp.beginning_of_line()
    l1 = lisp.point()
    lisp.end_of_line()
    l2 = lisp.point()
    line = lisp.buffer_substring(l1,l2)
    block_begin,block_end,content = get_block_content("```python","```")

    #lisp.message(content)
        
    # we have code content at this point

    # scan content to find plt.plot(). if there is, scan buffer
    # previous to *here* to determine order of _this_ plt.plot(), and
    # give it an appropiate index that will be appended to the end of
    # the .png image file, i.e. [buffer name]_[index].png. plt.plot()
    # commands will be replaced by the corresponding plt.savefig
    # command.

    # generate savefig for execution code (no output in emacs yet)
    bc = get_buffer_content_prev(block_begin)
    plt_count_before = len(re.findall('plt\.savefig\(',bc))
    base = os.path.splitext(lisp.buffer_name())[0]
    f = '%s_%s.png' % (base, two_digit(plt_count_before+1))
    rpl = "plt.savefig('%s')" % f
    show_replaced = True if "plt.show()" in content else False
    content=content.replace("plt.show()",rpl)
    include_graphics_command = "![](%s)" % f

    (kc,kernel,ip) = get_kernel_pointer(lisp.buffer_name())
    start = time.time()
    res = ''
    with capture_output() as io:
        ip.run_cell(content)
    res = io.stdout
    if kernel.shell.last_known_outflag:
        etype, value, tb = kernel.shell._get_exc_info()
        res = str(etype) + " " + str(value)  + "\n"        
    elapsed = (time.time() - start)
    # replace this unnecessary message so output becomes blank
    if res and len(res) > 0:  # if result not empty
        res = res.replace("Populating the interactive namespace from numpy and matplotlib\n","")
        display_results(block_end, res) # display it
    else:
        display_results(block_end, "") 
        lisp.goto_char(block_end)

    # generate includegraphics command
    if show_replaced:
        lisp.forward_line(2) # skip over end verbatim, leave one line emtpy
        lisp.insert(include_graphics_command + '\n')
        #lisp.backward_line_nomark(1) # skip over end verbatim, leave one line emtpy        
        lisp.scroll_up(1) # skip over end verbatim, leave one line emtpy        
        lisp.goto_char(remember_where)
        lisp.replace_string("plt.show()",rpl,None,block_begin,block_end)
        
    lisp.goto_char(remember_where)
    if "plt.savefig" in content: lisp.preview_buffer()
    
    lisp.message("Ran in " + str(elapsed) + " seconds")