Created
August 18, 2019 12:50
-
-
Save bergpb/05d30c3cf367bab19ae287b11be47a35 to your computer and use it in GitHub Desktop.
Get id with javascript in flask web app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from flask import Flask, render_template_string | |
| app = Flask(__name__) | |
| @app.route('/', methods=['GET']) | |
| def home(): | |
| data = [ | |
| {'id': 1, 'name': 'Nicola', 'age': 17}, | |
| {'id': 2, 'name': 'Josh', 'age': 17}, | |
| {'id': 3, 'name': 'Lua', 'age': 17}, | |
| ] | |
| return render_template_string(""" | |
| <html> | |
| <body> | |
| <table> | |
| <tr> | |
| {% for i in data %} | |
| <td>{{ i.name }}</td> | |
| <td>{{ i.age }}</td> | |
| <td><button onClick=getId('{{ i.id }}')>Get id</button></td> | |
| {% endfor %} | |
| </tr> | |
| </table> | |
| </body> | |
| <script> | |
| function getId(id){ | |
| console.log(`Returned id: ${id}`); | |
| } | |
| </script> | |
| </html> | |
| """, data=data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment