0

I have a fairly basic flask set up (been coding using sublime text). For testing purposes, I am now trying to run the following .py file. It contains a query that generates the data: user_data. I am trying to see what the result of that data is.

from datetime import datetime
from flask import Flask, render_template, url_for 
import sqlite3

app = Flask(__name__) 
db_locale='users.db'

def query_comments():
        connie=sqlite3.connect(db_locale)
        c=connie.cursor()
        c.execute("""
        SELECT * FROM comments  

        """)
        user_data=c.fetchall()
        print(user_data) #CHANGE THIS TO return user_data


@app.route('/') 
@app.route('/home')
def home():
    user_data=query_comments() #new variable user_Data that is the result from this query
    return render_template('home.html',user_data=user_data)

if __name__ == '__main__':
    app.run(debug=True)

On Editing with IDLE and running the file bigquestions.py, it comes up with the following error:

from flask import Flask, render_template, url_for
ModuleNotFoundError: No module named 'flask'

I solved this by opening it in the right python version. IDLE is not the same as what was installed in the venv. It now prints the results of the query correctly.

What still doesn't work:

  1. Have I made a mistake in the set up or code somewhere? The user_data is NOT being displayed on the webpage.

in the home.html (in templates)

<p><b>{{ sampletext }}</b></p>
<p>{{ user_data }}</p>

...but user_data is not being displayed on the webpage. The above data sampletext works fine, so it isn't a matter of rendering.

I think I've just missed a step or some code, and am not sure what.

The server returns NO errors

Compoot
  • 2,227
  • 6
  • 31
  • 63
  • maybe this thread https://stackoverflow.com/questions/29179631/edit-with-idle-option-missing-from-context-menu could help you – cizario Jun 25 '20 at 16:37
  • thanks - but it has nothing with Edit with IDLE being missing. That's not the issue at all - I'll have a look though. :) – Compoot Jun 25 '20 at 16:46
  • I solved the running (import part) - but the query doesn't work – Compoot Jun 25 '20 at 17:18

2 Answers2

1

try this code below

[..]
def query_comments():
    connie = sqlite3.connect(
        db_locale,
        detect_types=sqlite3.PARSE_DECLTYPES
    )
    user_data = connie.execute("""SELECT * FROM comments""").fetchall()
    connie.close()
    return user_data

[..]

refer to official Flask tutorial topic Define and Access the Database

the github repo

it seems you solved the part one but it looks like a hack not the right way.

it seems you have 2 versions of python on your windows system so you have to set the right environment system to avoid mixing between python versions and virtual environments

cizario
  • 3,995
  • 3
  • 13
  • 27
  • Thank you for this - I will consider and upvote. I've made a change to the question to clarify. The query is definitely generating the data. It is just not being displayed to the webpage. So user_data when used in the home.html page as {{ user_data }} is not displayed/ – Compoot Jun 25 '20 at 17:57
0

Part 1 solved: As updated above. It was coming up with the import error as I was opening it in IDLE which was defaulting to Python 3.7 and the venv was running 3.8. The files now run fine BUT Part 2 - the results of the query user_data are still not being printed. I'll accept any answer that explains this/points out the error

Part 2: Odd...I figured it out, but don't understand exactly why it worked. I essentially had to comment out another function above it that was also passing parameters to the home.html page. Is it not allowed to have multiple functions that pass parameters?

The function def send_comments() was commented out. All worked fine then!

Code with commented out function

from datetime import datetime
from flask import Flask,render_template,url_for

import sqlite3

app = Flask(__name__) 
db_locale='users.db'

"""

@app.route('/home')
def sendcomments():
    sampletext="Responses:" #sending this as a variable to home
    return render_template('home.html',sampletext=sampletext)
"""

@app.route('/') 
@app.route('/home')
def home():
    user_data=query_comments() #new variable user_Data that is the result from this query
    return render_template('home.html',user_data=user_data)


@app.route('/about') 
def about():
    return render_template('about.html')
"""
@app.route('/addcomment')
def addcomment():
    return render_template()
"""


def query_comments():
        connie = sqlite3.connect(db_locale)
        c=connie.cursor()
        c.execute("""
        SELECT * FROM comments  

        """)
        userdata=c.fetchall()
        return userdata


query_comments()

if __name__ == '__main__':
    app.run(debug=True)
Compoot
  • 2,227
  • 6
  • 31
  • 63