DEV Community

SHRAY SALVI
SHRAY SALVI

Posted on

How to connect Ipython with own frontend?

I am a college student working on a project to create a Python playground website where users can write and run Python code. I am using the Ace code editor for the frontend, and I believe I need to use IPython in the backend to provide a Python environment for users to execute their code.

However, I am unsure about how to proceed with integrating IPython into my website. Specifically, I need guidance on how to communicate with the IPython kernel and frontend. Additionally, I would like to set up isolated environments for each user using Docker, but I'm not sure how to implement this.

Also, I think the Jupyter client can be an easy way to communicate with Kernal.

Top comments (4)

Collapse
 
airo profile image
Airo

You can leverage a running IPython kernel and connect your frontend to it

Collapse
 
shraysalvi profile image
SHRAY SALVI

Here is my client.py file which I am going to use to connect with a kernel

from jupyter_client import KernelManager

def main():
    # Create a new kernel manager
    km = KernelManager(kernel_id='9912')
    km.start_kernel()

    # Create a client to interact with the kernel
    kc = km.client()
    kc.start_channels()

    # Ensure the client is connected before executing code
    kc.wait_for_ready()

    # Execute the code
    code = 'print("Hello, World!")'
    msg_id = kc.execute(code)

    # Wait for the result and display it
    while True:
        try:
            msg = kc.get_iopub_msg(timeout=1)
            print(msg)
            content = msg["content"]

            # When a message with the text stream comes and it's the result of our execution
            if msg["msg_type"] == "stream" and content["name"] == "stdout":
                print(content["text"])
                break
        except KeyboardInterrupt:
            print("Interrupted by user.")
            break
        except:
            # If no messages are available, we'll end up here, but we can just continue and try again.
            pass

    # Cleanup
    kc.stop_channels()
    km.shutdown_kernel()

if __name__ == '__main__':
    main()
Enter fullscreen mode Exit fullscreen mode

Now I am confused about whether KernelManager(kernel_id='9912') is connecting to my configured kernel or create one on its own.

Here is my complete process -

Step 1

Created a k_conf.json file to configure kernel

{
    "shell_port": 63101,
    "iopub_port": 63102,
    "stdin_port": 63103,
    "control_port": 63106,
    "hb_port": 63105,
    "ip": "127.0.0.1"
  }
Enter fullscreen mode Exit fullscreen mode

Then I used the command ipython kernel k_conf.json
Output:

NOTE: When using the `ipython kernel` entry point, Ctrl-C will not work.

To exit, you will have to explicitly quit this process, by either sending
"quit" from a client, or using Ctrl-\ in UNIX-like environments.

To read more about this, see https://github.com/ipython/ipython/issues/2049

To connect another client to this kernel, use:
    --existing kernel-9912.json
Enter fullscreen mode Exit fullscreen mode

TO check the output is basically generated from the kernel with k_conf.json configuration or not I need the log of created kernel.

Collapse
 
shraysalvi profile image
SHRAY SALVI

I think it is creating a new kernel, as it created a new file in my tmp/ directory, kernel-9912.json

Collapse
 
shraysalvi profile image
SHRAY SALVI

I can simply run the ipython kernel with the help of ipython kernel and Now I wanted to connect to that kernel and I believe jupyter_client would be beneficial in this case but I am unable to connect to the kernel.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay