help icon indicating copy to clipboard operation
help copied to clipboard

405 method not allowed of "PUT"

Open runhelu opened this issue 6 years ago • 0 comments

I wrote an extension called "new question" and it lives in tree dir of jupyter notebook. When clicking it, it will send a "PUT" request to api/contents and ask notebook to create a new .ipynb file. `define([ 'jquery', 'require', 'contents', 'base/js/namespace', 'base/js/utils', 'tree/js/newnotebook', 'base/js/i18n', 'base/js/dialog', 'base/js/events', 'services/config' ], function ($, require, contents_service, IPython, utils, newnotebook, i18n, dialog, events, config, Jupyter) { function load_ipython_extension() {

    var common_options = {
        base_url: $('body').data().baseUrl,
        notebook_path: $('body').data("notebookPath")
    };
    var cfg = new config.ConfigSection('tree', common_options);
    cfg.load();
    common_options.config = cfg;
    var common_config = new config.ConfigSection('common', common_options);
    common_config.load();
    var contents = new contents_service.Contents({
        base_url: common_options.base_url,
        common_config: common_config
    });

    function httpGetAsync(theUrl, callback) {
        var xmlHttp = new XMLHttpRequest();
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
                callback(JSON.parse(xmlHttp.responseText));
        }
        xmlHttp.open("GET", theUrl, true); // true for asynchronous
        xmlHttp.send(null);
    }

    var new_question = function (path, options) {
        var fileName = options.content.cells[0].metadata.question_id;
        var data = JSON.stringify({
            content: options.content,
            type: options.type
        });
        var settings = {
            processData: false,
            type: "PUT",
            data: data,
            contentType: 'application/json',
            dataType: "json"
        };
        return utils.promising_ajax(contents.api_url(path + '/' + fileName + '.ipynb'), settings);
    };

    var openIpynbForNewQuestion = function (ipynb_data) {
        var kernel_name = "python3";
        console.log(ipynb_data);
        var w = window.open(undefined, IPython._target);
        new_question(common_options.notebook_path, {type: "notebook", content: ipynb_data}).then(
            function (data) {

                var url = utils.url_path_join(
                    common_options.base_url, 'notebooks',
                    utils.encode_uri_components(data.path)
                );
                url += "?kernel_name=" + kernel_name;
                w.location = url;
            },
            function (error) {
                w.close();
                dialog.modal({
                    title: i18n.msg._('Creating Notebook Failed'),
                    body: i18n.msg.sprintf(i18n.msg._("The error was: %s"), error.message),
                    buttons: {'OK': {'class': 'btn-primary'}}
                });
            }
        );
    }

    var handlerInitializeNewMentorHubQuestion = function () {
        var theUrl = "http://localhost:5000/newQuestion";

        //var theUrl = "https://api.mentoracademy.org/newQuestion/";
        httpGetAsync(theUrl, openIpynbForNewQuestion);
    };

    var form = $('<div/>')
        .addClass('pull-right sort_button')
        .appendTo('#notebook_list_header');

    $('<button/>')
        .attr('type', 'button')
        .attr('id', 'new-mentor-hub')
        .addClass('btn btn-default btn-xs')
        .attr('data-toggle', 'button')
        .attr('title', 'Match case')
        .css('font-weight', 'bold')
        .text('New Question')
        .on('click', handlerInitializeNewMentorHubQuestion)
        .appendTo(form);
}

return {
    load_ipython_extension: load_ipython_extension
};

});`

It works well locally, but when I deploy it on jupyterhub using docker. It gives me a 405 error. It seems jupyterhub banned me from sending "PUT" request to the notebook api. Here is the Dockerfile I used:

`#FROM continuumio/anaconda3:latest

ARG BASE_IMAGE=jupyterhub/singleuser:0.8 FROM $BASE_IMAGE #create uwsgi user and group #RUN groupadd -r victor && useradd -r -g victor victor #change directory WORKDIR /

#copy install files files inside COPY extensions /extensions COPY requirements.txt / COPY fakefiles /fakefiles

#install dependency #USER root #RUN conda install --yes jupyter notebook #RUN conda update notebook RUN conda install --yes --file requirements.txt #RUN conda install -c conda-forge --yes jupyterhub #RUN apt-get -y update #RUN apt-get install -y python3-pip #RUN python3 -m pip install jupyterhub==0.8.1

WORKDIR /extensions RUN jupyter nbextension install . --user RUN jupyter nbextension enable disablenotebookextensions RUN jupyter nbextension enable importdataset RUN jupyter nbextension enable insertcell RUN jupyter nbextension enable newdataset RUN jupyter nbextension enable newpart RUN jupyter nbextension enable newquestion

WORKDIR / #USER root RUN cp ./fakefiles/notebook.json /home/jovyan/.jupyter/nbconfig RUN cp ./fakefiles/tree.json /home/jovyan/.jupyter/nbconfig

#WORKDIR /extensions RUN jupyter nbextension enable insertcell

WORKDIR /home/jovyan/

#EXPOSE 8888 #WORKDIR /notebooks

#CMD ["jupyter", "notebook", "--no-browser","--allow-root","--ip=0.0.0.0"]

`

runhelu avatar May 16 '18 14:05 runhelu