Python -Flask I want to display the data that present in Mongodb on a html page in clean format
I have a list of questions data in Mongodb database. I want to display question by question on html page. This is my Flask code
from flask import Flask, render_template, request, url_for
from pymongo import MongoClient
from bson.json_util import dumps
import json
client = MongoClient('localhost:27017')
db = client.girish
app = Flask(__name__)
@app.route("/questions", methods = ['GET'])
def questions():
try:
names = db.questions.find({},{"ques":1,"options":1,"_id":0,"quesid":1,"ans":1}).limit(10)
return render_template('index.html', names=names)
#return dumps(names)
except Exception as e:
return dumps({'error' : str(e)})
@app.route("/answers", methods = ['POST'])
def answers():
try:
data = json.loads(request.data)
quesid = data['quesid']
yourans = data['yourans']
if quesid and yourans:
status = db.myanswers.insert_one({
"quesid" : quesid,
"yourans" : yourans
})
return dumps({'message' : 'SUCCESS'})
except Exception as e:
return dumps({'error' : str(e)})
@app.route("/myanswers", methods = ['GET'])
def myanswers():
try:
answers = db.myanswers.find({},{"quesid":1,"yourans":1})
return dumps(answers)
except Exception as e:
return dumps({'error' : str(e)})
@app.route("/check/<id>", methods = ['GET','POST'])
def check(id):
try:
doc=db.questions.find({'quesid':float(id)})
return dumps(doc)
except Exception as e:
return dumps({'error': str(e)})
if __name__ == '__main__':
app.run(port='5002',debug = True)
What is the possible ways that I can display the data present in monodb on html page?? I'm new to python web development. please help me out in a detailed process.
This is my mogodb injection code with python
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["girish"]
mycol = mydb["questions"]
mylist = [
{
"ques" : "what is test?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 1,
"ans" : 1
},
{
"ques" : "what is test2?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 2,
"ans" : 1
},
{
"ques" : "what is test3?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 3,
"ans" : 1
},
{
"ques" : "what is test4?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 4,
"ans" : 1
},
{
"ques" : "what is test5?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 5,
"ans" : 1
},
]
x = mycol.insert_many(mylist)
How can I create a html page and how to link with my database?
python mongodb web flask flask-restful
add a comment |
I have a list of questions data in Mongodb database. I want to display question by question on html page. This is my Flask code
from flask import Flask, render_template, request, url_for
from pymongo import MongoClient
from bson.json_util import dumps
import json
client = MongoClient('localhost:27017')
db = client.girish
app = Flask(__name__)
@app.route("/questions", methods = ['GET'])
def questions():
try:
names = db.questions.find({},{"ques":1,"options":1,"_id":0,"quesid":1,"ans":1}).limit(10)
return render_template('index.html', names=names)
#return dumps(names)
except Exception as e:
return dumps({'error' : str(e)})
@app.route("/answers", methods = ['POST'])
def answers():
try:
data = json.loads(request.data)
quesid = data['quesid']
yourans = data['yourans']
if quesid and yourans:
status = db.myanswers.insert_one({
"quesid" : quesid,
"yourans" : yourans
})
return dumps({'message' : 'SUCCESS'})
except Exception as e:
return dumps({'error' : str(e)})
@app.route("/myanswers", methods = ['GET'])
def myanswers():
try:
answers = db.myanswers.find({},{"quesid":1,"yourans":1})
return dumps(answers)
except Exception as e:
return dumps({'error' : str(e)})
@app.route("/check/<id>", methods = ['GET','POST'])
def check(id):
try:
doc=db.questions.find({'quesid':float(id)})
return dumps(doc)
except Exception as e:
return dumps({'error': str(e)})
if __name__ == '__main__':
app.run(port='5002',debug = True)
What is the possible ways that I can display the data present in monodb on html page?? I'm new to python web development. please help me out in a detailed process.
This is my mogodb injection code with python
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["girish"]
mycol = mydb["questions"]
mylist = [
{
"ques" : "what is test?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 1,
"ans" : 1
},
{
"ques" : "what is test2?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 2,
"ans" : 1
},
{
"ques" : "what is test3?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 3,
"ans" : 1
},
{
"ques" : "what is test4?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 4,
"ans" : 1
},
{
"ques" : "what is test5?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 5,
"ans" : 1
},
]
x = mycol.insert_many(mylist)
How can I create a html page and how to link with my database?
python mongodb web flask flask-restful
add a comment |
I have a list of questions data in Mongodb database. I want to display question by question on html page. This is my Flask code
from flask import Flask, render_template, request, url_for
from pymongo import MongoClient
from bson.json_util import dumps
import json
client = MongoClient('localhost:27017')
db = client.girish
app = Flask(__name__)
@app.route("/questions", methods = ['GET'])
def questions():
try:
names = db.questions.find({},{"ques":1,"options":1,"_id":0,"quesid":1,"ans":1}).limit(10)
return render_template('index.html', names=names)
#return dumps(names)
except Exception as e:
return dumps({'error' : str(e)})
@app.route("/answers", methods = ['POST'])
def answers():
try:
data = json.loads(request.data)
quesid = data['quesid']
yourans = data['yourans']
if quesid and yourans:
status = db.myanswers.insert_one({
"quesid" : quesid,
"yourans" : yourans
})
return dumps({'message' : 'SUCCESS'})
except Exception as e:
return dumps({'error' : str(e)})
@app.route("/myanswers", methods = ['GET'])
def myanswers():
try:
answers = db.myanswers.find({},{"quesid":1,"yourans":1})
return dumps(answers)
except Exception as e:
return dumps({'error' : str(e)})
@app.route("/check/<id>", methods = ['GET','POST'])
def check(id):
try:
doc=db.questions.find({'quesid':float(id)})
return dumps(doc)
except Exception as e:
return dumps({'error': str(e)})
if __name__ == '__main__':
app.run(port='5002',debug = True)
What is the possible ways that I can display the data present in monodb on html page?? I'm new to python web development. please help me out in a detailed process.
This is my mogodb injection code with python
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["girish"]
mycol = mydb["questions"]
mylist = [
{
"ques" : "what is test?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 1,
"ans" : 1
},
{
"ques" : "what is test2?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 2,
"ans" : 1
},
{
"ques" : "what is test3?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 3,
"ans" : 1
},
{
"ques" : "what is test4?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 4,
"ans" : 1
},
{
"ques" : "what is test5?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 5,
"ans" : 1
},
]
x = mycol.insert_many(mylist)
How can I create a html page and how to link with my database?
python mongodb web flask flask-restful
I have a list of questions data in Mongodb database. I want to display question by question on html page. This is my Flask code
from flask import Flask, render_template, request, url_for
from pymongo import MongoClient
from bson.json_util import dumps
import json
client = MongoClient('localhost:27017')
db = client.girish
app = Flask(__name__)
@app.route("/questions", methods = ['GET'])
def questions():
try:
names = db.questions.find({},{"ques":1,"options":1,"_id":0,"quesid":1,"ans":1}).limit(10)
return render_template('index.html', names=names)
#return dumps(names)
except Exception as e:
return dumps({'error' : str(e)})
@app.route("/answers", methods = ['POST'])
def answers():
try:
data = json.loads(request.data)
quesid = data['quesid']
yourans = data['yourans']
if quesid and yourans:
status = db.myanswers.insert_one({
"quesid" : quesid,
"yourans" : yourans
})
return dumps({'message' : 'SUCCESS'})
except Exception as e:
return dumps({'error' : str(e)})
@app.route("/myanswers", methods = ['GET'])
def myanswers():
try:
answers = db.myanswers.find({},{"quesid":1,"yourans":1})
return dumps(answers)
except Exception as e:
return dumps({'error' : str(e)})
@app.route("/check/<id>", methods = ['GET','POST'])
def check(id):
try:
doc=db.questions.find({'quesid':float(id)})
return dumps(doc)
except Exception as e:
return dumps({'error': str(e)})
if __name__ == '__main__':
app.run(port='5002',debug = True)
What is the possible ways that I can display the data present in monodb on html page?? I'm new to python web development. please help me out in a detailed process.
This is my mogodb injection code with python
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["girish"]
mycol = mydb["questions"]
mylist = [
{
"ques" : "what is test?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 1,
"ans" : 1
},
{
"ques" : "what is test2?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 2,
"ans" : 1
},
{
"ques" : "what is test3?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 3,
"ans" : 1
},
{
"ques" : "what is test4?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 4,
"ans" : 1
},
{
"ques" : "what is test5?",
"options" : [
{
"op1" : "test",
"op2" : "test",
"op3" : "test"
}
],
"quesid" : 5,
"ans" : 1
},
]
x = mycol.insert_many(mylist)
How can I create a html page and how to link with my database?
python mongodb web flask flask-restful
python mongodb web flask flask-restful
asked Nov 22 '18 at 6:43
Girish TulabanduGirish Tulabandu
1
1
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
How can I create an HTML page?
Flask uses something called as jinja templates
which has a specific syntax. It combines HTML structure and your data to return an HTML page populated with data to be displayed in the browser
How to link with my database?
I see you are using PyMongo
to feed and read data from MongoDB which is perfect.
Now the linking part,
Create a python function to access data from database, put it into a python variable, use all required logic and then pass that python variable to the html template. You will have to provide a unique url which a web browser will use to invoke this function using @app.route
I am writing a sample code to display all questions from questions
collection
@app.route("/questions", methods = ['GET'])
def questions():
try:
questions = db.questions.find({},{"ques":1,"ans":1})
return render_template('questions.html', questions = questions)
except Exception as e:
return dumps({'error' : str(e)})
Now the template which renders data -
Jinja 2 template provides a way to display values of python variables in a html file. Jinja allows you to use conditional statements (if-else) as well as for loops for appropriate use cases.
Questions.html
<!doctype html>
<html>
<body>
{% for question in questions %}
<h3>{{question["ques"]}}</h3>
{% endfor %}
</body>
</html>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425222%2fpython-flask-i-want-to-display-the-data-that-present-in-mongodb-on-a-html-page%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
How can I create an HTML page?
Flask uses something called as jinja templates
which has a specific syntax. It combines HTML structure and your data to return an HTML page populated with data to be displayed in the browser
How to link with my database?
I see you are using PyMongo
to feed and read data from MongoDB which is perfect.
Now the linking part,
Create a python function to access data from database, put it into a python variable, use all required logic and then pass that python variable to the html template. You will have to provide a unique url which a web browser will use to invoke this function using @app.route
I am writing a sample code to display all questions from questions
collection
@app.route("/questions", methods = ['GET'])
def questions():
try:
questions = db.questions.find({},{"ques":1,"ans":1})
return render_template('questions.html', questions = questions)
except Exception as e:
return dumps({'error' : str(e)})
Now the template which renders data -
Jinja 2 template provides a way to display values of python variables in a html file. Jinja allows you to use conditional statements (if-else) as well as for loops for appropriate use cases.
Questions.html
<!doctype html>
<html>
<body>
{% for question in questions %}
<h3>{{question["ques"]}}</h3>
{% endfor %}
</body>
</html>
add a comment |
How can I create an HTML page?
Flask uses something called as jinja templates
which has a specific syntax. It combines HTML structure and your data to return an HTML page populated with data to be displayed in the browser
How to link with my database?
I see you are using PyMongo
to feed and read data from MongoDB which is perfect.
Now the linking part,
Create a python function to access data from database, put it into a python variable, use all required logic and then pass that python variable to the html template. You will have to provide a unique url which a web browser will use to invoke this function using @app.route
I am writing a sample code to display all questions from questions
collection
@app.route("/questions", methods = ['GET'])
def questions():
try:
questions = db.questions.find({},{"ques":1,"ans":1})
return render_template('questions.html', questions = questions)
except Exception as e:
return dumps({'error' : str(e)})
Now the template which renders data -
Jinja 2 template provides a way to display values of python variables in a html file. Jinja allows you to use conditional statements (if-else) as well as for loops for appropriate use cases.
Questions.html
<!doctype html>
<html>
<body>
{% for question in questions %}
<h3>{{question["ques"]}}</h3>
{% endfor %}
</body>
</html>
add a comment |
How can I create an HTML page?
Flask uses something called as jinja templates
which has a specific syntax. It combines HTML structure and your data to return an HTML page populated with data to be displayed in the browser
How to link with my database?
I see you are using PyMongo
to feed and read data from MongoDB which is perfect.
Now the linking part,
Create a python function to access data from database, put it into a python variable, use all required logic and then pass that python variable to the html template. You will have to provide a unique url which a web browser will use to invoke this function using @app.route
I am writing a sample code to display all questions from questions
collection
@app.route("/questions", methods = ['GET'])
def questions():
try:
questions = db.questions.find({},{"ques":1,"ans":1})
return render_template('questions.html', questions = questions)
except Exception as e:
return dumps({'error' : str(e)})
Now the template which renders data -
Jinja 2 template provides a way to display values of python variables in a html file. Jinja allows you to use conditional statements (if-else) as well as for loops for appropriate use cases.
Questions.html
<!doctype html>
<html>
<body>
{% for question in questions %}
<h3>{{question["ques"]}}</h3>
{% endfor %}
</body>
</html>
How can I create an HTML page?
Flask uses something called as jinja templates
which has a specific syntax. It combines HTML structure and your data to return an HTML page populated with data to be displayed in the browser
How to link with my database?
I see you are using PyMongo
to feed and read data from MongoDB which is perfect.
Now the linking part,
Create a python function to access data from database, put it into a python variable, use all required logic and then pass that python variable to the html template. You will have to provide a unique url which a web browser will use to invoke this function using @app.route
I am writing a sample code to display all questions from questions
collection
@app.route("/questions", methods = ['GET'])
def questions():
try:
questions = db.questions.find({},{"ques":1,"ans":1})
return render_template('questions.html', questions = questions)
except Exception as e:
return dumps({'error' : str(e)})
Now the template which renders data -
Jinja 2 template provides a way to display values of python variables in a html file. Jinja allows you to use conditional statements (if-else) as well as for loops for appropriate use cases.
Questions.html
<!doctype html>
<html>
<body>
{% for question in questions %}
<h3>{{question["ques"]}}</h3>
{% endfor %}
</body>
</html>
edited Nov 25 '18 at 15:40
Ankur
1534
1534
answered Nov 25 '18 at 8:29
Bhavani RaviBhavani Ravi
721423
721423
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425222%2fpython-flask-i-want-to-display-the-data-that-present-in-mongodb-on-a-html-page%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown