how to get response from python to HTML

Options
ananth
ananth Registered Posts: 41 ✭✭✭✭✭

Hi,

how to get response or error from python to HTML and alert that message in HTML/JS?

for e.g if i create project using python API create_project(project_key, name, owner, description=None, settings=None, project_folder_id=None) and this successfully created or failed how to get the response back to HTML and alert as successfully created or failed with exception as error message in alert box etc?

Thanks,

Ananth

Best Answer

  • Liev
    Liev Dataiker Alumni Posts: 176 ✭✭✭✭✭✭✭✭
    Answer ✓
    Options

    Hi @ananth

    I don't see your definition in backend for endpoint first_api_call. You can use both the browser console debugger and the Log of the backend to make sure that:

    - The backend is being called and reached with parameters

    - The backend is processing correctly the request.

    - The backend is responding with something valid and that the Front end is receiving this.

    I would put loggers and print statements along the way in order to follow that chain of events.

Answers

  • Liev
    Liev Dataiker Alumni Posts: 176 ✭✭✭✭✭✭✭✭
    edited July 17
    Options

    Hi @ananth

    If your API call succeeds, when you create the project using the Python public API, you will do so like this:

    dataikuapi.dss.project.DSSProject

    as documented here . The new `new_project` object will be of type

    import dataiku
    client = dataiku.api_client()
    new_project = client.create_project('TEST_PROJECT', 'test project', 'tester', description='a simple description')
    If the creation of the project failed, you will receive an error stating the reason for it.
  • ananth
    ananth Registered Posts: 41 ✭✭✭✭✭
    edited July 17
    Options

    Hi @Liev
    ,

    thank you for the details project creation was successful and logs as below

    2020-06-29 10:50:14,216 INFO 127.0.0.1 - - [29/Jun/2020 10:50:14] "GET /__ping HTTP/1.1" 200 -
    {"requesttype": "DSS Project Creation Form", "Email": "a", "CountryRegion": ""}
    2020-06-29 10:50:18,332 INFO 127.0.0.1 - - [29/Jun/2020 10:50:18] "GET /first_api_call?
    description=a&requesttype=DSS+Project+Creation+Form&CntryRegn= HTTP/1.1" 200 -

    in python

    data = json.dumps({"Email": userdescription, "requesttype": requesttype, "CountryRegion": countryRegion})
    return json.dumps({"status": "ok", "data": data})

    in HTML

    $.getJSON(getWebAppBackendUrl('first_api_call'), {
    "description": descriptionText.value, "requesttype": requesttypetext
    , "CntryRegn": lstCountryorRegionval
    })
    .done(
    function (data) {
    clearMessage();
    displayMessage(data);
    }
    );

    function displayMessage(messageText, messageClassname) {
    var obj = JSON.parse(messageText);
    alert(obj);
    var messageContainer = document.getElementById('message');
    messageContainer.innerHTML = messageText;
    //messageContainer.className = '';

    }

    its not displaying the alert message from python return response

  • ananth
    ananth Registered Posts: 41 ✭✭✭✭✭
    Options

    Hi @Liev
    ,

    thank you! my bad , issue was with actually with clearMessage() function, what this function does as below, so it was clearing those, as soon as i commented it worked and able to get the data from python to HTML

    function clearMessage() {
    displayMessage('');
    }

    .done(
    function (data) {
    //clearMessage();
    displayMessage(data);
    }
    );

Setup Info
    Tags
      Help me…