コード例 #1
0
ファイル: views.py プロジェクト: kustomzone/mljar-studio
 def create_new_script_session(self, project_id, script_id):
     kernel_id = None
     kernel_execution_state = None
     with transaction.atomic():
         jc = JupyterClient()
         kernel = jc.start_kernel()
         if kernel is not None:
             ScriptSession(
                 kernel_id=kernel["id"],
                 parent_organization_id=1,
                 parent_project_id=project_id,
                 parent_script_id=script_id,
             ).save()
             kernel_id = kernel["id"]
             kernel_execution_state = kernel["execution_state"]
     return kernel_id, kernel_execution_state
コード例 #2
0
ファイル: views.py プロジェクト: kustomzone/mljar-studio
    def perform_create(self, serializer):
        print("perform create")
        try:
            with transaction.atomic():
                instance = serializer.save(created_by=1,
                                           parent_organization_id=1,
                                           slug="tmp_slug")
                instance.slug = "{0}_{1}".format(slugify(instance.title),
                                                 instance.id)
                instance.save()

                jc = JupyterClient()
                if not jc.create_project_directory(instance.slug):
                    raise Exception(
                        "Cannot create project directory in the Jupyter")

        except Exception as e:
            raise APIException(str(e))
コード例 #3
0
    def get(self, request):

        url_arr = request.META["HTTP_REFERER"].split(":")
        url = "http:" + url_arr[1] + ":8888"
        ws_url = url_arr[1][2:] + ":8888"

        os.environ["JUPYTER_URL"] = url
        os.environ["JUPYTER_WS_URL"] = ws_url
        os.environ["JUPYTER_TOKEN"] = "my_very_secret_token"

        print("JUPYTER URLs")
        print("REST API:", os.environ["JUPYTER_URL"])
        print("WS:", os.environ["JUPYTER_WS_URL"])

        jc = JupyterClient()

        if not jc.is_available():
            return Response(
                "Connection to Jupyter is not available",
                status=status.HTTP_503_SERVICE_UNAVAILABLE,
            )

        if not jc.can_authenticate():
            return Response(
                "Cannot autheticate in Jupyter. Please check your Jupyter token configuration",
                status=status.HTTP_403_FORBIDDEN,
            )

        return Response({
            "username":
            "******",
            "organizations": [{
                "slug": "personal",
                "name": "Personal"
            }],
            "jupyter": {
                "url": os.environ.get("JUPYTER_URL"),
                "ws": os.environ.get("JUPYTER_WS_URL"),
                "token": os.environ.get("JUPYTER_TOKEN"),
            },
        })
コード例 #4
0
ファイル: runner.py プロジェクト: zeta1999/herqooly
def go_runner(script_id):

    try:
        runner = Runner(script_id)
        if len(runner.cells) == 0:
            return

        script_path = "sandbox/" + runner.script.parent_project.slug + "/src"
        script_name = runner.script.slug + "_queue_runner.ipynb"
        script_path += "/" + script_name
        jc = JupyterClient()
        j_session = JupyterClient().start_session(path=script_path,
                                                  name=script_name)
        kernel = j_session["kernel"]

        connection_url = "{}/api/kernels/{}/channels?session_id={}".format(
            os.environ["JUPYTER_WS_URL"], kernel["id"], runner.session_id)
        headers = {"Authorization": "Token " + os.environ["JUPYTER_TOKEN"]}

        print(connection_url)

        websocket.enableTrace(True)
        ws = websocket.WebSocketApp(
            connection_url,
            header=headers,
            on_message=lambda ws, msg: runner.on_message(ws, msg),
            on_error=lambda ws, error: runner.on_error(ws, error),
            on_close=lambda ws: runner.on_close(ws),
            on_open=lambda ws: runner.on_open(ws),
        )

        ws.run_forever()

        jc.shutdown_kernel(kernel["id"])
    except Exception as e:
        raise Exception(
            "Exception during executing script (id={}), details: {}".format(
                script_id, str(e)))
コード例 #5
0
ファイル: views.py プロジェクト: kustomzone/mljar-studio
    def perform_destroy(self, instance):

        try:
            with transaction.atomic():

                file_path = None
                if instance.location == "uploaded":
                    file_path = "/".join([
                        instance.parent_project.slug,
                        "data",
                        "uploaded",
                        instance.file_name,
                    ])
                if file_path is not None:
                    jc = JupyterClient()
                    if not jc.delete_file(file_path):
                        raise Exception("Cannot delete file in the Jupyter")

                instance.delete()

        except Exception as e:
            print(e)
            raise APIException(str(e))
コード例 #6
0
ファイル: views.py プロジェクト: kustomzone/mljar-studio
    def post(self,
             request,
             organization_slug,
             project_id,
             script_id,
             format=None):

        try:
            script = Script.objects.get(pk=script_id)
        except ObjectDoesNotExist as e:
            return Response(status=status.HTTP_404_NOT_FOUND)

        cells = self.request.data.get("cells")
        plain_code = ""
        jupyter_nb_code = []
        for cell in cells:
            plain_code += cell["code"] + "\n"
            jupyter_nb_code += [{
                "metadata": {
                    "trusted": True,
                    "cellUid": cell["cellUid"]
                },
                "cell_type": "code",
                "source": cell["code"],
                "execution_count": None,
                "outputs": [],
            }]

        print("plain_code")
        print(plain_code)

        file_name = script.slug + ".py"
        file_path = "sandbox/" + script.parent_project.slug + "/src/"

        script.script_file = file_path + file_name

        print("Plain code", file_name, file_path)
        jc = JupyterClient()
        jc.write_file(file_name, file_path, plain_code)

        file_name = script.slug + ".ipynb"
        file_path = "sandbox/" + script.parent_project.slug + "/src/"

        script.notebook_file = file_path + file_name

        print("Jupyter Nb", file_name, file_path)
        jc = JupyterClient()
        jc.write_jupyter_nb_file(file_name, file_path, jupyter_nb_code)

        script.code = json.dumps(cells)
        script.save()

        current_widgets = Widget.objects.filter(parent_script=script)
        current_widgets_map = {}
        # do a dict mapping
        for w in current_widgets:
            current_widgets_map[w.widgetUid] = w

        widgets = self.request.data.get("widgets", [])

        for _, widget in widgets.items():
            if widget["widgetUid"] in current_widgets_map:
                # do update
                w = current_widgets_map[widget["widgetUid"]]
                w.widget_type = widget["widget_type"]
                w.data = json.dumps(widget["data"])
                w.layout = json.dumps(widget["layout"])
                w.style = json.dumps(widget["style"])
                w.visible = widget["visible"]
                w.save()
            else:  # create new widget
                w = Widget(
                    widgetUid=widget["widgetUid"],
                    widget_type=widget["widget_type"],
                    data=json.dumps(widget["data"]),
                    layout=json.dumps(widget["layout"]),
                    style=json.dumps(widget["style"]),
                    parent_script=script,
                    parent_project=script.parent_project,
                    cellUid=widget["cellUid"],
                    visible=widget["visible"],
                )
                w.save()

        return Response(status=status.HTTP_201_CREATED)
コード例 #7
0
ファイル: views.py プロジェクト: kustomzone/mljar-studio
    def post(self,
             request,
             organization_slug,
             project_id,
             script_id,
             format=None):

        action = self.request.data.get("action")  # start, shutdown
        if not action in ["start", "shutdown", "interrupt", "restart"]:
            return Response(status=status.HTTP_404_NOT_FOUND)
        kernel_id = self.request.data.get("kernelId")

        kernels_running = JupyterClient().get_kernels()
        if kernels_running is None:
            return Response(status=status.HTTP_404_NOT_FOUND)
        for k in kernels_running:
            print("Running: {}".format(k["id"]))

        #session = ScriptSession.objects.filter(
        #    parent_project__id=project_id, parent_script__id=script_id
        #)

        if action == "start":

            ###
            try:
                script = Script.objects.get(pk=script_id)
            except ObjectDoesNotExist as e:
                return Response(status=status.HTTP_404_NOT_FOUND)

            script_path = "sandbox/" + script.parent_project.slug + "/src"
            script_name = script.slug + ".ipynb"
            script_path += "/" + script_name
            ses = JupyterClient().start_session(path=script_path,
                                                name=script_name)
            print("SESSION", ses)
            return Response({
                "id": ses["kernel"]["id"],
                "execution_state": ses["kernel"]["execution_state"]
            })
            ###

        elif action == "shutdown":
            for s in session:
                jc = JupyterClient()
                kernel = jc.shutdown_kernel(s.kernel_id)
                s.delete()
            return Response(status=status.HTTP_204_NO_CONTENT)

        elif action == "interrupt":
            if kernel_id is None:
                return Response(status=status.HTTP_404_NOT_FOUND)
            jc = JupyterClient()
            if jc.interrupt_kernel(kernel_id):
                return Response(status=status.HTTP_200_OK)
            else:
                return Response(status=status.HTTP_404_NOT_FOUND)

        elif action == "restart":
            if kernel_id is None:
                return Response(status=status.HTTP_404_NOT_FOUND)
            jc = JupyterClient()
            if jc.restart_kernel(kernel_id):
                return Response(status=status.HTTP_200_OK)
            else:
                return Response(status=status.HTTP_404_NOT_FOUND)

        return Response(status=status.HTTP_404_NOT_FOUND)