AnnotatorJS Store with Python

196 views Asked by At

I am using Annotatorjs library and I want to combine it with the Annotator-Store Plugin to run it on an Python Flask Server.

But i don't know how to send the data from the Javascript to Python and receive it there, so that i can store the annotated data in an database.

My Javascript Code Looks like:

//load tag plugin
content.annotator('addPlugin', 'Tags');

//load store plugin for save and retrieve annotations
content.annotator('addPlugin', 'Store', {
    // The endpoint of the store on your server.
    prefix: 'http://localhost/annotations',

    // Attach the uri of the current page to all annotations to allow search.
    annotationData: {
        'uri': 'http://localhost/'
    },
    urls: {
        // These are the default URLs.
        create:  '/annotations',
        update:  '/annotations/:id',
        destroy: '/annotations/:id',
        search:  '/search'
  }

    // This will perform a "search" action when the plugin loads. Will
    // request the last 20 annotations for the current url.
    // eg. /store/endpoint/search?limit=20&uri=http://this/document/only
    loadFromSearch: {
        'limit': 100,
        'all_fields': 1,
        'uri': 'http://localhost/'
    },

    showViewPermissionsCheckbox: false,

    showEditPermissionsCheckbox: false
});
});

But how can i receive the data in my Python file?

1

There are 1 answers

0
Abhijeetk431 On

You can do a post request from JavaScript and then use POST method in your python app file to receive data in your python file.

From Javascript, using jQuery(A function like this is called to Post Data):-

function post_data(data1,data2) {
    $.post("endpoint/url", {
        "myData1": data1
        "myData2": data2
    }, location.reload())
}

Inside your Flask Application file:-

@app.route('/endpoint/url', methods=['POST'])
def save_data():
    data1 = request.form['myData1']
    data2 = request.form['myData2']
    save_them_function(data1,data2)
    return redirect(url_for('home'))