GithubHelp home page GithubHelp logo

Comments (4)

cytec avatar cytec commented on June 7, 2024

for searching and stuff, here is how i did it:

function that handles searching:

def perform_search(queryset, user_input):
    return queryset.filter(
        db.or_(
            models.Processed.title.like('%'+user_input+'%'),
            models.Processed.user.like('%'+user_input+'%'),
            models.Processed.platform.like('%'+user_input+'%')
            )
        )

in the view:

table.searchable(lambda queryset, user_input: perform_search(queryset, user_input))

afaik reflection should provide you with a "mapped class" when loaded, have you tried? basically giving the t from your view function above as Class object to Datatables? something like this (untested)

@theapp.route('/upload/preview/', methods=['GET', 'POST'])
def preview(tablename):
    m = db.MetaData()
    t = db.Table(tablename, m, autoload = True, autoload_with = db.engine)
    table = DataTable(request.args, t, db.query(t), [
        "id",
        ("name", "full_name", lambda i: "User: {}".format(i.full_name)),
        ("address", "address.description"),
    ])
    table.searchable(lambda queryset, user_input: perform_search(queryset, user_input))

    ....

from datatables.

okyere avatar okyere commented on June 7, 2024

cytec, good question. I actually use sqlalchemy extensively in my application; just not this part of the application. See, I'm building a data upload tool. User uploads and excel sheet; the data is taken and written to database. I have models for department, datasource, fields, and field types. Whenever there's a business need for a customer to send me data, new entries for department name, the name of the data source, the column/fields in the data and the types (number, text, etc) those columns have need to be defined. So sqlalchemy takes care of those. When the user actually uploads the data, it goes to a table whose name is one of the attributes of the datasource model. So the tables are created dynamically every time. That is why I have to use reflection to get the table object before querying it. There's not a way to dynamically create models hence my implementation.

Back to my question, I'm trying to add a functionality to the application to where the user can get a preview of the data they just uploaded; and I think datatables plugin will do a good job at it. But I'm doing things in flask and not php or pyramid.

from datatables.

orf avatar orf commented on June 7, 2024

Hey there,
It's been a while since I looked at the code, but your use case should be fully supported. Interesting application by the way :)

So @cytec has it right as far as I can see (table.searchable(lambda queryset, user_input: perform_search(queryset, user_input)) should be just table.searchable(perform_search) though!). This library is completely framework agnostic, so the examples being in pyramid don't matter. The Datatables class just takes a dictionary-like object (request.form in flask), a model (so it can get the column definitions), a queryset (so you can pre-filter the results) and a list of columns. None of this has to come from any specific source, and dynamically generating it is an interested case that I hadn't thought of.

So the issue is, can you use Datatables with reflected tables. I've never used them before so I can't answer that, but @cytec's answer is reasonable. The second parameter doesn't have to be a SQLAlchemy model, it just has to be an object with some columns on it. You can get the columns via the .columns (or .c) attribute on any Table. I think this is a list though, so you could either make an object each time with the correct columns:

class FakeModel(object):
   pass

model = FakeModel()
for col in your_table_object.
    column_name = col.name # Or something, probably not right
    setattr(model, column_name, col)

d = DataTables(request.form, model, ...)

Or you could make a pull request to support accepting a list of columns as the second parameter, the only time it is used is in the get_column() function.

Hope this helps!

from datatables.

okyere avatar okyere commented on June 7, 2024

Thanks guys. Hard as I tried, I couldn't get this to work.

from datatables.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.