≡

Using Python backend

Using Python backend

praabyepraabye Posts: 7Questions: 4Answers: 0

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem: Hi!

I'm trying to use a Python backend, but I need some more documentation.

A complete example with .html, .js and relevant parts of the .py would be really nice. I've looked through published examples on datatables.net, but do not find anything like this.

I have imported the relevant libraries in my .py, I hope:

from sqlalchemy import create_engine
from datatables_server import Editor, Field, DataTable, Column

Answers

  • kthorngrenkthorngren Posts: 22,525Questions: 26Answers: 5,174
    edited November 6

    There aren't any Python docs on this site. The docs for the Python library (datatables_server) should have config samples, etc. Are you using this library?

    Essentially you would configure a route, example from the above library's docs:

    @app.route("/api/staff", methods=["GET"])
    def staff():
        with engine.connect() as db:
            table = (
                DataTable(db, "staff", "id")
                .column(Column("first_name"))
                .column(Column("last_name"))
                .column(Column("position"))
                .column(Column("salary"))
            )
            table.process(request.values)
            response = table.data().to_dict()
        return jsonify(response)
    

    Then configure the Datatable to use ajax and build the table using the JSON repsonse, like this example pointing to your route. Like this:

    new DataTable('#example', {
        ajax: '/api/staff',
        columns: [
            { data: 'first_name' },
            { data: 'last_name' },
            { data: 'position' },
            { data: 'salary' }
        ]
    });
    

    Datatables and the Editor don't have knowledge of the backend so their docs won't change due to using Python. Refer to the Python library's docs for setting up the server side code.

    I can try to anwer any Python questions you have but this forum isn't setup for Python support. Please let us know of any Datatables or Editor questions you have.

    Kevin

  • allanallan Posts: 66,190Questions: 1Answers: 11,042 Site admin

    A small correction, the documentation for the DataTables Python libraries is available here and you can download a demo Editor application for Python here. That includes all of the Editor demos (nearly all of the other examples on the site are client-side specific configuration, and thus are backend independent).

    The one catch at the moment is that the upload for the Python libraries doesn't work. That is something I'll work on.

    Allan

  • kthorngrenkthorngren Posts: 22,525Questions: 26Answers: 5,174

    Wow, I didn't know you created a Python library. That's great!

    Kevin

  • praabyepraabye Posts: 7Questions: 4Answers: 0
    edited November 9

    Thank you for a good answer. I'll be working on it!
    Peter

    ...and yes, it's the Python library you referred to,, I'm trying to learn. (After some years with the PHP solution).

  • allanallan Posts: 66,190Questions: 1Answers: 11,042 Site admin

    New server, or just for a chance of scene? :)

  • praabyepraabye Posts: 7Questions: 4Answers: 0

    No, not just change of scene. Trying to weed out PHP on our system.

    BTW: The datetime picker is really good. I can manually enter dates back to the 1500's as I need to. But how do I show that range of years in the picker? (Yes, I know: Julian and Gregorian dates.... But for this purpose we don't really care!)

  • allanallan Posts: 66,190Questions: 1Answers: 11,042 Site admin

    The year range option is the one that controls the list of dates shown in the drop down. I will say up front though, that the date picker isn't particularly well optimised for dates outside 25 years or so from now, since it results in a really long select list.

    I was going to suggest looking at the <input type="date"> tag, but, at least in Firefox, its year input is even worse.

    You might want to take a look around at various date pickers and see what would work best for you - I've seen some that let you "zoom out" for example, doing back to pick a century, then decade, then year, which might work better for you. Creating a field type plugin for Editor is a relatively simple process.

    Allan

  • praabyepraabye Posts: 7Questions: 4Answers: 0

    Thanks! I'll look into that!
    Peter

Sign In or Register to comment.