gaby
gaby

Reputation: 1

I keep getting this error: "jinja2.exceptions.TemplateNotFound: index.html"

i tried to save index.html file in template folder but still it does work. any help would be highly appreciated. here is my code: am using sypder.

#importing neccessary libs

import numpy as np
from flask import Flask,request,render_template
from flask_cors import CORS
import os
import joblib
import pickle
import flask
import newspaper
from newspaper import Article
import urllib
import nltk
nltk.download('punkt')

#loading flash and assigning the model variable

app= Flask(__name__)
CORS(app)
app=flask.Flask(__name__,template_folder='templates')

 #handle
with open('model.pk1','rb') as handle:
model= pickle.load(handle)
@app.route('/')

def main(): return render_template('templates/index.html')

#receiving the input url from the user and using web scrapping to 
extract th news content

@app.route('/predict',methods=['GET','POST'])
def predict():
    url=request.get_data(as_text=True)[5:]
    url=urllib.parse.unquote(url)
    article=Article(str(url))
    article.download()
    article.parse()
    article.nlp()
    news=article.summary

    #passing the news article to the model and returning whether it 
    is Fake or Real

    pred=model.predict([news])
    return render_template('index.html', prediction_text='The news 
     is "{}"'.format(pred[0]))
if __name__=="__main__":
    port=int(os.environ.get('PORT',5000))
    app.run(port=port,debug=True, use_reloader=False)

Upvotes: 0

Views: 382

Answers (1)

Josh Liu
Josh Liu

Reputation: 476

I'm not sure how is your project structure, but there could be two possible reasons:

  1. This can be changed:

    app= Flask(__name__)    
    CORS(app)    
    app=flask.Flask(__name__,template_folder='templates')
    

    into:

    app= Flask(__name__, template_folder='templates')    
    CORS(app)
    

    And since you defend the template folder as templates, return render_template('templates/index.html') should change to return render_template('index.html'). Otherwise you're rendering templates/templates/index.html.

  2. Another reason could be the path. Since it is relative path, do think if it can be ../templates or ../../templates.

Upvotes: 1

Related Questions