def python_bokeh():
    utils.bokehGarbageCollector()
    # retrieve input parameters
    script = request.get_json()['script']
    widget_id = request.get_json()['widget_id']
    script_file_name = constants.TMP_FOLDER + "bokeh_script_" + str(widget_id) + ".txt"
    user_id = utils.retrieveKnowageInfo(request.headers)
    dataset_name, datastore_request = utils.retrieveDatasetInfo(request.get_json())
    drivers = utils.retrieveAnalyticalDriversInfo(request.get_json())
    python_widget = PythonWidgetExecution(analytical_drivers=drivers, script=script, user_id=user_id,
                                          dataset_name=dataset_name, datastore_request=datastore_request, widget_id=widget_id)
    # check authentication
    if not security.userIsAuthorizedForFunctionality(python_widget, constants.EDIT_PYTHON_SCRIPTS):
        return "Error: authentication failed", 401
    #destroy old bokeh server
    if utils.serverExists(python_widget.widget_id):
        utils.destroyServer(python_widget.widget_id)
    # resolve analytical drivers
    for d in drivers:
        python_widget.script = python_widget.script.replace("$P{" + d + "}", "drivers_.get(\'" + d + "\')")
    # retrieve dataset
    if python_widget.dataset_name != None:
        dataset_file = constants.TMP_FOLDER + python_widget.dataset_name + ".pckl"
        df = utils.getDatasetAsDataframe(python_widget)
        df.to_pickle(dataset_file)
        python_widget.script = "import pandas as pd\n" + python_widget.dataset_name + " = pd.read_pickle(\"" + dataset_file + "\")\n" + python_widget.script

    #function executed by bokeh server
    def modify_doc(doc):
        # retrieve script from file
        with open(script_file_name, "r") as bk_file:
            bk_script = bk_file.read()
        #replace curdoc() with keyword "curdoc_"
        bk_script = bk_script.replace("curdoc()", "curdoc_")
        # execute script
        namespace = {'curdoc_': doc, "drivers_": drivers}
        exec(bk_script, namespace)

    #secondary thread function (bokeh server)
    def bk_worker():
        server = Server({'/bkapp'+str(python_widget.widget_id): modify_doc}, io_loop=IOLoop(), allow_websocket_origin=["*"], port=cuncurrency_manager.bokeh_resources[python_widget.widget_id].port)
        with cuncurrency_manager.lck:
            cuncurrency_manager.active_servers.update({python_widget.widget_id:server}) #{widget_id : bokeh_server}
        server.start()
        server.io_loop.start()

    #flush script content to file so that modify_doc() can retrieve the code to be executed
    with open(script_file_name,"w") as bokeh_file:
        bokeh_file.write(python_widget.script)

    #instance a bokeh server for the widget if not instanciated yet
    if not utils.serverExists(python_widget.widget_id): #allocate bokeh server
        t = Thread(target=bk_worker) #thread that hosts bokeh server
        bk_res = BokehResourceList(thread=t, timestamp=datetime.now(), port=utils.findFreePort(), dataset_name=dataset_name)
        with cuncurrency_manager.lck:
            cuncurrency_manager.bokeh_resources.update({python_widget.widget_id : bk_res}) #{widget_id : BokehResourceList}
        t.start()
    #serve plot
    jscript = server_document(utils.getPythonAddress() + ":" + str(cuncurrency_manager.bokeh_resources[python_widget.widget_id].port) + "/bkapp" + str(python_widget.widget_id))
    return render_template("embed.html", script=jscript)
def python_bokeh():
    # retrieve input parameters
    script = request.get_json()['script']
    widget_id = request.get_json()['widget_id']
    script_file_name = constants.TMP_FOLDER + "bokeh_script_" + str(
        widget_id) + ".txt"
    user_id, knowage_address = utils.retrieveKnowageInfo(
        request.headers, request.get_json())
    dataset_name, datastore_request = utils.retrieveDatasetInfo(
        request.get_json())
    # check authentication
    if not security.userIsAuthenticated(user_id, knowage_address):
        return "Error: authentication failed", 401
    #destroy old bokeh server
    if utils.serverExists(widget_id):
        utils.destroyServer(widget_id)
    # retrieve dataset
    if dataset_name != "":
        dataset_file = constants.TMP_FOLDER + dataset_name + ".pckl"
        df = utils.getDatasetAsDataframe(user_id, knowage_address,
                                         dataset_name, datastore_request)
        df.to_pickle(dataset_file)
        script = "import pandas as pd\n" + dataset_name + " = pd.read_pickle(\"" + dataset_file + "\")\n" + script

    #function executed by bokeh server
    def modify_doc(doc):
        # retrieve script from file
        with open(script_file_name, "r") as bk_file:
            bk_script = bk_file.read()
        #replace curdoc() with keyword "curdoc_"
        bk_script = bk_script.replace("curdoc()", "curdoc_")
        # execute script
        namespace = {'curdoc_': doc}
        exec(bk_script, namespace)

    #secondary thread function (bokeh server)
    def bk_worker():
        server = Server({'/bkapp' + str(widget_id): modify_doc},
                        io_loop=IOLoop(),
                        allow_websocket_origin=["localhost:8080"],
                        port=cuncurrency_manager.ports_dict[widget_id])
        with cuncurrency_manager.lck:
            cuncurrency_manager.active_servers.update(
                {widget_id: server})  #{widget_id : bokeh_server}
        server.start()
        server.io_loop.start()

    #flush script content to file so that modify_doc() can retrieve the code to be executed
    with open(script_file_name, "w") as bokeh_file:
        bokeh_file.write(script)

    #instance a bokeh server for the widget if not instanciated yet
    if not utils.serverExists(widget_id):  #allocate bokeh server
        t = Thread(target=bk_worker)  #thread that hosts bokeh server
        with cuncurrency_manager.lck:
            cuncurrency_manager.active_threads.update(
                {widget_id: t})  #{widget_id : thread}
            cuncurrency_manager.ports_dict.update({
                widget_id:
                utils.findFreePort()
            })  #{widget_id : port_number_of_bokeh_server}
        t.start()
    #serve plot
    jscript = server_document("http://localhost:" +
                              str(cuncurrency_manager.ports_dict[widget_id]) +
                              "/bkapp" + str(widget_id))
    return render_template("embed.html", script=jscript)
def python_bokeh():
    # retrieve input parameters
    document_id, widget_id = utils.retrieveWidgetInfo(request.get_json())
    script_file_name = constants.TMP_FOLDER + "bokeh_script_" + str(
        widget_id) + ".txt"
    user_id, knowage_address = utils.retrieveKnowageInfo(
        request.headers, request.get_json())
    dataset_name, datastore_request = utils.retrieveDatasetInfo(
        request.get_json())
    drivers = utils.retrieveAnalyticalDriversInfo(request.get_json())
    python_widget = PythonWidgetExecution(analytical_drivers=drivers,
                                          user_id=user_id,
                                          document_id=document_id,
                                          widget_id=widget_id,
                                          knowage_address=knowage_address,
                                          dataset_name=dataset_name,
                                          datastore_request=datastore_request)
    python_widget.script = security.loadScriptFromDB(python_widget)
    #destroy old bokeh server
    if utils.serverExists(python_widget.widget_id):
        utils.destroyServer(python_widget.widget_id)
    # resolve analytical drivers
    for d in drivers:
        python_widget.script = python_widget.script.replace(
            "$P{" + d + "}", "drivers_.get(\'" + d + "\')")
    # retrieve dataset
    if python_widget.dataset_name != "":
        dataset_file = constants.TMP_FOLDER + python_widget.dataset_name + ".pckl"
        df = utils.getDatasetAsDataframe(python_widget)
        df.to_pickle(dataset_file)
        python_widget.script = "import pandas as pd\n" + python_widget.dataset_name + " = pd.read_pickle(\"" + dataset_file + "\")\n" + python_widget.script

    #function executed by bokeh server
    def modify_doc(doc):
        # retrieve script from file
        with open(script_file_name, "r") as bk_file:
            bk_script = bk_file.read()
        #replace curdoc() with keyword "curdoc_"
        bk_script = bk_script.replace("curdoc()", "curdoc_")
        # execute script
        namespace = {'curdoc_': doc, "drivers_": drivers}
        exec(bk_script, namespace)

    #secondary thread function (bokeh server)
    def bk_worker():
        server = Server(
            {'/bkapp' + str(python_widget.widget_id): modify_doc},
            io_loop=IOLoop(),
            allow_websocket_origin=["localhost:8080"],
            port=cuncurrency_manager.ports_dict[python_widget.widget_id])
        with cuncurrency_manager.lck:
            cuncurrency_manager.active_servers.update(
                {python_widget.widget_id: server})  #{widget_id : bokeh_server}
        server.start()
        server.io_loop.start()

    #flush script content to file so that modify_doc() can retrieve the code to be executed
    with open(script_file_name, "w") as bokeh_file:
        bokeh_file.write(python_widget.script)

    #instance a bokeh server for the widget if not instanciated yet
    if not utils.serverExists(python_widget.widget_id):  #allocate bokeh server
        t = Thread(target=bk_worker)  #thread that hosts bokeh server
        with cuncurrency_manager.lck:
            cuncurrency_manager.active_threads.update(
                {python_widget.widget_id: t})  #{widget_id : thread}
            cuncurrency_manager.ports_dict.update({
                python_widget.widget_id:
                utils.findFreePort()
            })  #{widget_id : port_number_of_bokeh_server}
        t.start()
    #serve plot
    jscript = server_document(
        "http://localhost:" +
        str(cuncurrency_manager.ports_dict[python_widget.widget_id]) +
        "/bkapp" + str(python_widget.widget_id))
    return render_template("embed.html", script=jscript)