librelist archives

« back to archive

flask autocompletion

flask autocompletion

From:
Tomáš Pelka
Date:
2014-01-21 @ 12:13
Hi all,

I went through several of howtos about enabling autocompletion via jquery
but still kind of stucked on one think. I hope I can get any advices how to
make my code work.

python code:
# autocompletion
@app.route('/autocomplete', methods=['GET'])
def autocomplete():
    results = []
    search = request.args.get('term')
    print search

results.append(db.Execution.select().where(db.regexp(db.Execution.identifier,
search)))
    return jsonify(results)

form.py:
class DiffFrom(Form):
    identifierA = TextField('identifierA',
        validators=[validators.Optional()])

template:
<!-- jquery -->
    <link rel="stylesheet" href="
http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
    <script src="{{ url_for('static', filename='js/jquery.js') }}"></script>
    <script src="{{ url_for('static', filename='js/jquery-ui.js')
}}"></script>
    <script src="{{ url_for('static', filename='js/bootstrap.min.js')
}}"></script>

    <script type=text/javascript>
    $SCRIPT_ROOT = {{ request.script_root|tojson|safe }};
    </script>

    <script type="text/javascript">
    $(function() {
        $.ajax({
            url: '{{ url_for("autocomplete") }}'
        }).done(function (data) {
            $('#identifierA').autocomplete({
                source: data,
                minLength: 2
            });
        });
    })
    </script>

<form class="form-inline" role="form" name="form" method="post">
    {{ form.hidden_tag() }}
    <!-- identifier input field -->
         {{ form.identifierA(size=10, placeholder="Identifier",
class="form-control", title="Regexp accepted") }}
 </form>

The print in #automation outputs None, which is not correct IMHO. Does
anybody know what am I doing wrong?

Thanks

Tomáš Pelka

Re: [flask] flask autocompletion

From:
Farhan Ahmed
Date:
2014-01-21 @ 13:33
Change your app route to:

@app.route('/autocomplete/<term>', methods=['GET'])
...

And your signature of 'autocomplete' method to:

def autocomplete(term):
...

Best,
Farhan

> On Jan 21, 2014, at 6:13 AM, "Tomáš Pelka" <tompelka@gmail.com> wrote:
> 
> Hi all,
> 
> I went through several of howtos about enabling autocompletion via 
jquery but still kind of stucked on one think. I hope I can get any 
advices how to make my code work.
> 
> python code:
> # autocompletion
> @app.route('/autocomplete', methods=['GET'])
> def autocomplete():
>     results = []
>     search = request.args.get('term')
>     print search
>     
results.append(db.Execution.select().where(db.regexp(db.Execution.identifier,
search)))
>     return jsonify(results)
> 
> form.py:
> class DiffFrom(Form):
>     identifierA = TextField('identifierA',
>         validators=[validators.Optional()])
> 
> template:
> <!-- jquery -->
>     <link rel="stylesheet" 
href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
>     <script src="{{ url_for('static', filename='js/jquery.js') }}"></script>
>     <script src="{{ url_for('static', filename='js/jquery-ui.js') }}"></script>
>     <script src="{{ url_for('static', filename='js/bootstrap.min.js') 
}}"></script>
> 
>     <script type=text/javascript>
>     $SCRIPT_ROOT = {{ request.script_root|tojson|safe }};
>     </script>
> 
>     <script type="text/javascript">
>     $(function() {
>         $.ajax({
>             url: '{{ url_for("autocomplete") }}'
>         }).done(function (data) {
>             $('#identifierA').autocomplete({
>                 source: data,
>                 minLength: 2
>             });
>         });
>     })
>     </script>
> 
> <form class="form-inline" role="form" name="form" method="post">
>     {{ form.hidden_tag() }}
>     <!-- identifier input field -->
>          {{ form.identifierA(size=10, placeholder="Identifier", 
class="form-control", title="Regexp accepted") }}
>  </form>
> 
> The print in #automation outputs None, which is not correct IMHO. Does 
anybody know what am I doing wrong?
> 
> Thanks
> 
> Tomáš Pelka