TexteaInc / funix

Building web apps without manually creating widgets
http://funix.io
MIT License
89 stars 10 forks source link

Support for Jupyter notebooks #86

Open ahuang11 opened 1 month ago

ahuang11 commented 1 month ago

Wondering if it's possible to run in notebook, or at least indirectly.

In Panel, there's panel_obj.show() which opens a new browser.

forrestbao commented 1 month ago

Thanks @ahuang11 for this suggestion. We heard the same feature request from many others at PyCon US 2024.

What's your suggestion on how a user can trigger the code-to-app conversion? As you know, Funix does not require the user to modify the code. So how can Jupyter know when is the time to map the user code to a GUI app? By adding a magic command and display the app when pressing Ctrl/Cmd + Enter? Or, by using a new button on Jupyter?

ahuang11 commented 1 month ago

A decorator probably?

@funix
def ...

Or magic command works too I guess, but I like decorator

%funix

def ...
forrestbao commented 1 month ago

Thanks. I am sitting on the fence between the two options. I feel the magic command is more a Juypte-way of doing things. The downside is that if you want to save the code from Jupyter cells to regular Python scripts, you need to comment out the magic commands.

ahuang11 commented 1 month ago

Perhaps the most Jupyter way of doing things is if the function is on the last line. If it is, display it.

i.e.

def hello(your_name: str) -> str:
    return f"Hello, {your_name}."

hello
forrestbao commented 1 month ago

Thanks. We will support at least one way to do it first. Then we can support more.

Yazawazi commented 1 month ago

On the develop branch, there is a way to run Funix indirectly in Jupyter:

from funix import funix

@funix()
def hello(a: str) -> str:
    return f"Hello {a}!"

This will directly open a new Flask application and show this by iframe.

Image

In the future, I will try to communicate with the kernel for calling functions.

The following is a reference steps for using funix with the develop branch:

  1. git clone https://github.com/TexteaInc/funix
  2. cd funix
  3. git checkout develop
  4. pip install -e .
ahuang11 commented 1 month ago

Cool, quick update!

I'm wondering if it's possible to, separately, import funix.notebook once at an upper cell, and all subsequent cells will automatically show iframe if there's the function variable at the end?

Cell 1

import funix.notebook

Cell 2

def hello(a: str) -> str:
    return f"Hello {a}!"

hello
Yazawazi commented 1 month ago

I'll try to work on this idea 👀

forrestbao commented 1 month ago

I am not sure about this idea below because it conflicts with expectations per Jupyter convention. A user's expectation for the last line, hello is printing the variable hello, which here will be the location in the RAM of the function.

What do you guys think?

Cool, quick update!

I'm wondering if it's possible to, separately, import funix.notebook once at an upper cell, and all subsequent cells will automatically show iframe if there's the function variable at the end?

Cell 1

import funix.notebook

Cell 2

def hello(a: str) -> str:
    return f"Hello {a}!"

hello
ahuang11 commented 1 month ago

which here will be the location in the RAM of the function.

I personally have never utilized the location in the RAM of the function. Also, if they really need it, print(hello) would return that.

Alternatively, I think you could inject a method on all functions like:

hello.servable() or hello.funix() to make it show, depending on whether you want users to modify their code.

Also, Jupyter conventions I think, more accurately, displays the variable in a pretty format e.g.

image
forrestbao commented 1 month ago

The expected behavior is as follows:

In [1]: def hello(a: str) -> str:
   ...:     return "Hello, " + a + "!"
   ...: 

In [2]: hello
Out[2]: <function __main__.hello(a: str) -> str>

In [3]: print(hello)
<function hello at 0x70565a7a2320>
ahuang11 commented 1 month ago

Yes, I think display(hello) results in <function __main__.hello(a: str) -> str>

image