-
-
Save Revisto/a6f54b107a794b261a7bf50bd5bb42af to your computer and use it in GitHub Desktop.
pass object to javascript via json in flask
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Hello, World</title> | |
</head> | |
<body> | |
<p>Hello, <span id="username"></span></p> | |
<script> | |
var user = JSON.parse('{{ user | tojson | safe}}'); | |
document.getElementById('username').innerHTML=user.firstname + " " + | |
user.lastname; | |
</script> | |
</body> | |
</html> |
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 | |
app = Flask(__name__) | |
@app.route('/') | |
def hello_world(): | |
user = {'firstname': 'Harry', 'lastname': 'Potter'} | |
return render_template("index.html", user=user) | |
if __name__ == '__main__': | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment