Render Template won’t work for some reason

  docker, flask, python

Please help I’m following a tutorial for flask but every time I try to build up this one application the route returns an internal server error and the problem seems to be with "render template".

Here’s how I structured my app:

"python"
|_books.py
|_DockerFile

|
|_templates
index.html

Here’s the code in each of the files.

-> books.py

from flask import Flask, render_template
from flask_restful import Resource, Api
import requests

app = Flask(__name__)
api = Api(app)

@app.route('/')
def Hello():
    return '''<h1> Reading Archive</h1>
<p>A what would u like to read today?.</p>'''

@app.route('/books') #route 2
def books():
    return render_template("index.html")
 
if __name__ == '__main__':
    app.run('0.0.0.0','3333')

->DockerFile

FROM python:3
ADD books.py /
RUN pip install flask
RUN pip install requests
RUN pip install flask_restful
EXPOSE 3333
CMD [ "python", "./books.py"]

-> index.html

<!DOCTYPE html>
<html>
 <head>
    <title>
        A Small Library!
    </title>
 </head>
 <body>
    <h1>** Library ** </h1>
    <h3><u> Books in the Library:</u></h3>

    <p><b>Title: </b><i>Astrophysics For People in a Hurry</i> <br>
        <b>Author: </b> Neil DeGrasse Tyson <br>
        <b>About:</b> Basic questions about the universe put into simple words by everyone's favourite scientist. <br>
        <b>Year published: </b> 2017 <br>
        <b>Genre: </b> Science, Non-Fiction. <br>
        </p>

        <p><b>Title: </b><i>The Shadow over Innsmouth</i><br>

            <b>Author: </b> H.P Lovecraft<br>
            <b>About:</b> A small fishing town in Massachusetts hides a dark secret underneath its shores.<br>
            <b>Year published: </b> 1936<br>
            <b>Genre: </b> Lovecraftian Horror.<br>
            </p>
</body>
</html>


The HTML file works perfectly fine on its own and the first Route from books.py works fine except with I type /books in Docker.
Please note I’m super new to Flask and I’m still learning. Thanks if you’ve answered!

Source: Python Questions

LEAVE A COMMENT