nteract / hydrogen

:atom: Run code interactively, inspect data, and plot. All the power of Jupyter kernels, inside your favorite text editor.
https://nteract.gitbooks.io/hydrogen/
MIT License
3.92k stars 334 forks source link

Python: Running function definitions with decorators ignores the decorator #77

Closed radix closed 5 years ago

radix commented 9 years ago

Say we have this code:

def deco(f):
    return lambda x: f(x + 1)

@deco
def myfunc(x):
    return 1 / x

myfunc(0)

Trying to evaluate the myfunc function will be wrong in two different ways: if I have my cursor on the @deco line, it causes a syntax error because it seems to just be trying to evaluate @deco by itself, without the function definition after it. If I have my cursor on the def myfunc line, or on the blank line immediately after myfunc, it defines the function without the decorator.

I can work around this by manually selecting the whole function definition including the decorator line and then hitting cmd-alt-enter.

willwhitney commented 9 years ago

Hydrogen relies on Atom's code-folding and indentation right now to figure out what a block is. This is so that the code for finding blocks works across languages.

This is definitely annoying behavior, but I don't know of a way to address this without writing a parser for each language that Hydrogen supports. Which maybe we should do at some point, but isn't going to happen for a while.

Any ideas about how to solve this without breaking the problem out into all the languages?

radix commented 9 years ago

sorry, I don't have any ideas. is it a bug in atom?

willwhitney commented 9 years ago

Nope, it's just hard to take a bunch of text and figure out what a logical block is cross-language. Atom is correct not to fold to decorators, as that would fold the actual function definition under.

alvis commented 6 years ago

any suggestion to get around this problem?

n-riesco commented 6 years ago

@alvis A way around this is to use comments to define the execution cells in your code and use the command Hydrogen Run Cell and Move Down (Alt+Shift+Enter) to run them. E.g.:

image

alvis commented 6 years ago

@n-riesco: Thanks for pointing out. It's smart to use the # In[]: syntax to separate cells. Never thought it'd be a way.

mbroedl commented 6 years ago

Please have a look if the most recent commit in this PR nikitakit/hydrogen-python#10 work for you. I've just added exactly this functionality.

kylebarron commented 5 years ago

This is as solved as possible through the use of cells and with hydrogen-python so I'm going to close.