【问题标题】:embedding jupyter notebook/ google colab in Django app在 Django 应用程序中嵌入 jupyter notebook/google colab
【发布时间】:2022-01-10 16:59:51
【问题描述】:

我想建立一个网站并将能够创建单元格并在其中运行 python 代码的 jupyter notebook 功能嵌入到我的网站中

为了创建一个网站,我正在使用 Django 我想嵌入 google collab 或 jupyter notebook

顺便说一句,我已经进行了足够的研究,并且一直被 * 链接卡住,那里没有关于这个或他们想在 jupyter notebook 中使用 django 的答案

提前感谢你们提供的任何指导或任何参考。

【问题讨论】:

标签: python django jupyter-notebook


【解决方案1】:

您有很多选择:

注意::我用的是“jupyter-lab”,你可以用“jupyter notebook”

1- 重定向到“jupyter notebook”的第一个选项

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 "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)

如果你想在模板中实现它而不是重定向,你可以在Django模板中使用以下代码:

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

2- 第二个选项:

只需使用 jupyter notebook 命令 通过使用这个subprocess.check_output("your command".split())

【讨论】:

  • 我还没试过今天试试你的解决方案
  • 感谢它通过 django 启动了 jupyter 实验室,但它不支持多用户
  • 你可以看这里:github.com/jupyterhub/jupyterhub
【解决方案2】:

先导出再导入,看here解释清楚

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

【讨论】:

  • 我正在寻找一种解决方案,使用它我可以真正让人们在线编码和运行,而无需去任何其他平台
最近更新 更多