Skip to content

Instantly share code, notes, and snippets.

@mosdevly
Created March 13, 2020 17:55
Show Gist options
  • Select an option

  • Save mosdevly/c8555189cc634c58b60e05e7fbe82ebc to your computer and use it in GitHub Desktop.

Select an option

Save mosdevly/c8555189cc634c58b60e05e7fbe82ebc to your computer and use it in GitHub Desktop.

Revisions

  1. Proto created this gist Mar 13, 2020.
    10 changes: 10 additions & 0 deletions example.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    {% extends 'base.html' %}


    {% block content %}
    <script src="{{ url_for('static', filename='js/example.js') }}"></script>

    <div id="users">

    </div>
    {% endblock %}
    11 changes: 11 additions & 0 deletions example.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    $(function() {
    const userDiv = $('#users');

    $.get('api/users', (res) => {
    for (user of res) {
    console.log(user);
    userDiv.append(`<li>${user.username}</li>`);
    }
    });

    });
    12 changes: 12 additions & 0 deletions routes.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    @app.route('/example')
    def example():
    """Example of using sqlalchemy to return JSON."""
    return render_template('example.html')


    @app.route('/api/users')
    def get_all_users():
    """Example of using sqlalchemy to return JSON."""
    users = User.query.all()

    return jsonify([u.serialize() for u in users])