简体   繁体   English

在 Django 应用程序中嵌入 jupyter notebook/google colab

[英]embedding jupyter notebook/ google colab in Django app

I wanted to build a website and embed the jupyter notebook functionality of being able to create cells and run python code within it into my website我想建立一个网站并将能够创建单元格并在其中运行 python 代码的 jupyter notebook 功能嵌入到我的网站中

For creating a website I m using Django and I would like to embed either the google collab or jupyter notebook为了创建一个网站,我使用 Django 并且我想嵌入 google collab 或 jupyter notebook

By the way I have researched enough and have been stuck with the StackOverflow links where there no answer about this or the one where they want to use django in jupyter notebook顺便说一句,我已经进行了足够的研究,并且一直坚持使用 StackOverflow 链接,其中没有关于此的答案或他们想在 jupyter 笔记本中使用 django 的链接

Thanks in advance for any guidance or any reference that you guys can provide.提前感谢你们可以提供的任何指导或任何参考。

You have many options to do that:你有很多选择来做到这一点:

Note:: I used "jupyter-lab" you can use "jupyter notebook"注意::我使用“jupyter-lab”你可以使用“jupyter notebook”

1- The first option to redirect to "jupyter notebook" 1-重定向到“jupyter notebook”的第一个选项

django view.py django view.py

from django.shortcuts import redirect,HttpResponse
import subprocess
import time

def open_jupiter_notbook(request):
    b= subprocess.check_output("jupyter-lab list".split()).decode('utf-8')
    if "9999" not in b:
        a=subprocess.Popen("jupyter-lab  --no-browser --port 9999".split())
    start_time = time.time()
    unreachable_time = 10
    while True and "9999" not in b:
        timer = time.time()
        elapsed_time = timer-start_time
        b= subprocess.check_output("jupyter-lab list".split()).decode('utf-8')
        if "9999" in b:
            break
        if elapsed_time > unreachable_time:
            return HttpResponse("Unreachable")
    path = b.split('\n')[1].split('::',1)[0]
    #You can here add data to your path if you want to open file or anything
    return redirect(path)

if you want to implement it in template instead of redirect, you can use the following code in Django template:如果你想在模板中实现它而不是重定向,你可以在 Django 模板中使用以下代码:

<iframe src="{% url 'open_jupiter_notbook' %}" width= 600px height=200px></iframe>

2- The second option: 2-第二个选项:

just use jupyter notebook commands by using this subprocess.check_output("your command".split())只需使用此 subprocess.check_output subprocess.check_output("your command".split())即可使用 jupyter notebook 命令

Export it first and then import it, look here for a good explanation先导出再导入,看这里好解释

from google.colab import files
src = list(files.upload().values())[0]
open('mylib.py','wb').write(src)
import mylib

There is a medium article explaining how to do that here .有一篇中型文章解释了如何在此处执行此操作。 The gist:要点:

  1. Install django_extensions安装 django_extensions
  2. Add it to your INSTALLED_APPS将其添加到您的INSTALLED_APPS
  3. Run python manage.py shell_plus --notebook - you can run it in the background if you want jupiter to be accessible all the time.运行python manage.py shell_plus --notebook - 如果您希望 jupiter 始终可以访问,您可以在后台运行它。
  4. You can redirect to the jupyter URL from django您可以从 django 重定向到 jupyter URL

A jupyter notebook is basically a kernel that runs (interactive) python or any other language on the backend and a web based frontend that communicates with this kernel. A jupyter notebook is basically a kernel that runs (interactive) python or any other language on the backend and a web based frontend that communicates with this kernel. This communication is done by using zeromq.这种通信是通过使用 zeromq 完成的。 Under the hood it is websockets.在引擎盖下是 websockets。 The backend can run any other language as desired, eg Julia.后端可以根据需要运行任何其他语言,例如 Julia。 The front end can be other things beyond web, like a terminal console or qt console (in this case however we would not call it jupyter notebook because the notebook part in the name stands for the web interface).前端可以是 web 以外的其他东西,例如终端控制台或 qt 控制台(在这种情况下,我们不会将其称为 jupyter notebook,因为名称中的 notebook 部分代表 Z2567A5EC9705EB7AC2C984033E 接口)。

Server Side服务器端

In the python server side you will have to start the jupyter notebook server.在 python 服务器端,您必须启动 jupyter notebook 服务器。 Here we are using port 8123. We generate a random token that will have to be used in the client-side as well.这里我们使用端口 8123。我们生成一个随机令牌,客户端也必须使用该令牌。

pip3 install notebook
from uuid import uuid4
from notebook.notebookapp import NotebookApp

PORT = 8123
TOKEN = str(uuid4())

app = NotebookApp()
app.initialize(["--port", f"{PORT}", f"--NotebookApp.token={TOKEN}"])

(response under construction - show server handler) (正在建设的响应 - 显示服务器处理程序)

(full example in https://gist.github.com/bollwyvl/bd56b58ba0a078534272043327c52bd1 ) (在https://gist.github.com/bollwyvl/bd56b58ba0a078534272043327c52bd1中的完整示例)

Client Side客户端

All client-side behaviour you see in a Jupyter notebook is implemented in javascript.您在 Jupyter 笔记本中看到的所有客户端行为都在 javascript 中实现。 By client-side behaviour it is meant new cell creation, printing of results, showing graphs, establishing javascript side of communication et cetera.客户端行为是指创建新的单元格、打印结果、显示图表、建立 javascript 端的通信等等。 If you wish all of this then you would need almost如果您希望所有这些,那么您几乎需要

If you plan to have all of this behaviour then you would be better served by using an iframe including the regular jupyter notebook.如果您打算拥有所有这些行为,那么使用iframe包括常规的 jupyter 笔记本会更好。 If you plan having something simplier like having only a single cell and a full notebook is not necessary then you could implement a simple client by yourself.如果您计划拥有更简单的东西,例如只有一个单元并且不需要完整的笔记本,那么您可以自己实现一个简单的客户端。 For intermediary solutions you could also take parts of the already done jupyter notebook page and take some parts of it for you (btw it uses BSD license).对于中间解决方案,您还可以获取已经完成的 jupyter notebook 页面的一部分并为您获取其中的一部分(顺便说一句,它使用 BSD 许可证)。

Simple client side implemented by yourself自己实现的简单客户端

We are talking about client side, but since the client code has to be sent to client from server, still in the server you will have to instruct django to send the following html and javascript:我们谈论的是客户端,但是由于客户端代码必须从服务器发送到客户端,所以仍然在服务器中您必须指示 django 发送以下 html 和 javascript:

(response under construction) (响应正在建设中)

Full third party client side完整的第三方客户端

(response under construction) (响应正在建设中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM