R
Railway•13mo ago
curious_coder

Flask App deployed (using Docker); TemplateNotFound error

Hey guys, I'm aware this is primarily a Flask issue however this works locally. The docker container is successfully being built and deployed via railway.app but whenever I visit the URL, it tells me it cannot find index.html in the templates folder. I've even gone as far as manually specifying the templates folder location for the Flask app via :
main = Blueprint("main", __name__, template_folder="templates")
main = Blueprint("main", __name__, template_folder="templates")
Any ideas?
75 Replies
Percy
Percy•13mo ago
Project ID: 564afc01-1a48-4844-a9fb-394914380c11
curious_coder
curious_coderOP•13mo ago
564afc01-1a48-4844-a9fb-394914380c11 Any questions, please ask. https://dwellist.up.railway.app/
Brody
Brody•13mo ago
try using absolute paths
curious_coder
curious_coderOP•13mo ago
I did; even got that working locally but it just spits out the same error message on deployment
Brody
Brody•13mo ago
is the file in github? if so, do the file and folder names have the same casing?
curious_coder
curious_coderOP•13mo ago
yes I've even just thrown this in
def find_index_html(start_path="/app/app"):
for root, dirs, files in os.walk(start_path):
print(root, file=sys.stderr)
if "index.html" in files:
return str(os.path.join(root, "index.html"))


@main.route("/")
def index():
index_path = find_index_html()
print(find_index_html(), file=sys.stderr)
# Does index.html exist?
exist = os.path.exists(index_path)
if not exist:
print(f"NOT EXIST", file=sys.stderr)
else:
print(f"EXIST\n\n", file=sys.stderr)
return render_template(index_path)
def find_index_html(start_path="/app/app"):
for root, dirs, files in os.walk(start_path):
print(root, file=sys.stderr)
if "index.html" in files:
return str(os.path.join(root, "index.html"))


@main.route("/")
def index():
index_path = find_index_html()
print(find_index_html(), file=sys.stderr)
# Does index.html exist?
exist = os.path.exists(index_path)
if not exist:
print(f"NOT EXIST", file=sys.stderr)
else:
print(f"EXIST\n\n", file=sys.stderr)
return render_template(index_path)
Brody
Brody•13mo ago
and what's the outcome
curious_coder
curious_coderOP•13mo ago
Just a moment Okay, so it says it exists but it can't find the file locally However, I think that's due to the absolute path for templates. Scratch that, it still hates it Working directory is '/app' file path is '/app/app/templates/index.html'
Brody
Brody•13mo ago
your project is placed into an /app folder at the root of the container. but it shouldn't matter where your project is, your code should be able to work out the working directory and combine that with the relative paths to make absolute paths
curious_coder
curious_coderOP•13mo ago
I'm just going to change the root dir name from app to my project name dwellist So path will be dwellist/app/templates/index.html
Brody
Brody•13mo ago
you can't change where nixpacks puts your code
curious_coder
curious_coderOP•13mo ago
What do you mean
def find_index_html(start_path="/dwellist/app"):
for root, dirs, files in os.walk(start_path):
if "index.html" in files:
result = str(os.path.join(root, "index.html"))
return result


@main.route("/")
def index():
index_path = find_index_html()
print(f"Found index.html in: {find_index_html()}", file=sys.stderr)

# Does index.html exist?
exist = os.path.exists(index_path)
if not exist:
print(f"{index_path} DOES NOT EXIST", file=sys.stderr)
else:
print(f"{index_path} DOES EXIST\n\n", file=sys.stderr)
return render_template(index_path)
def find_index_html(start_path="/dwellist/app"):
for root, dirs, files in os.walk(start_path):
if "index.html" in files:
result = str(os.path.join(root, "index.html"))
return result


@main.route("/")
def index():
index_path = find_index_html()
print(f"Found index.html in: {find_index_html()}", file=sys.stderr)

# Does index.html exist?
exist = os.path.exists(index_path)
if not exist:
print(f"{index_path} DOES NOT EXIST", file=sys.stderr)
else:
print(f"{index_path} DOES EXIST\n\n", file=sys.stderr)
return render_template(index_path)
This evaluation returns true; the file at index_path does exist and yet
curious_coder
curious_coderOP•13mo ago
No description
curious_coder
curious_coderOP•13mo ago
# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory to /app
WORKDIR /dwellist

# Copy the current directory contents into the container at /dwellist
COPY . /dwellist

# Install latest pip
RUN python -m pip install --upgrade pip

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 5000 available to the world outside this container
EXPOSE 5000

# Run app.py when the container launches
CMD ["python", "app.py"]
# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory to /app
WORKDIR /dwellist

# Copy the current directory contents into the container at /dwellist
COPY . /dwellist

# Install latest pip
RUN python -m pip install --upgrade pip

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 5000 available to the world outside this container
EXPOSE 5000

# Run app.py when the container launches
CMD ["python", "app.py"]
curious_coder
curious_coderOP•13mo ago
No description
curious_coder
curious_coderOP•13mo ago
It's right friggin there
@main.route("/")
def index():
return render_template("index.html")
@main.route("/")
def index():
return render_template("index.html")
I've returned to this which works locally...
Brody
Brody•13mo ago
ah you are using a dockerfile, that would have been good information to have
curious_coder
curious_coderOP•13mo ago
Sorry, my bad That's the only way I've been deploying; It's the first time I'm deploying with Docker like this
Brody
Brody•13mo ago
dockerfile*
curious_coder
curious_coderOP•13mo ago
Perhaps I've set it up incorrectly? I assumed if it worked locally, it can work anywhere
Brody
Brody•13mo ago
does this work locally when you build and run the dockerfile?
curious_coder
curious_coderOP•13mo ago
Clearly a bad assumption on my part Yep
Brody
Brody•13mo ago
and I don't mean just python app.py actually running the image
curious_coder
curious_coderOP•13mo ago
So I do: docker built -t dwellist -f docker/Dockerfile . To run it docker run -p 5000:5000 dwellist index.html loads right up But when deployed; it poops itself
Brody
Brody•13mo ago
are you sure railway is using your dockerfile to build?
curious_coder
curious_coderOP•13mo ago
It should be, I had to manually specify the location via the Variables in the settings Dockerfile is in a docker folder at working directory
curious_coder
curious_coderOP•13mo ago
No description
Brody
Brody•13mo ago
do the build logs confirm it's using a dockerfile and not nixpacks?
curious_coder
curious_coderOP•13mo ago
No description
Brody
Brody•13mo ago
is railway using the same root directory that you use when you build the image?
curious_coder
curious_coderOP•13mo ago
That's what I'm not too sure about; I could add a print statement to find out? Unsure if that'd be an accurate representation but hey. Maybe there's another way to check? Workdir is set to dwellist
Brody
Brody•13mo ago
by default railway uses the root of your project, what path do you use locally
curious_coder
curious_coderOP•13mo ago
No description
Brody
Brody•13mo ago
this doesn't really have anything to do with the dockerfile
curious_coder
curious_coderOP•13mo ago
Surely that would be it right? It's creating a new folder in the container called dwellist and copying all local files/folders to that new folder
Brody
Brody•13mo ago
not quite what I'm asking
curious_coder
curious_coderOP•13mo ago
I'm not sure how to answer the question 😬 Locally, I just execute from within dwellist It's been a long day man Cannot absord words
Brody
Brody•13mo ago
can you share your repo?
curious_coder
curious_coderOP•13mo ago
Sure
curious_coder
curious_coderOP•13mo ago
GitHub
GitHub - a-curious-coder/dwellist
Contribute to a-curious-coder/dwellist development by creating an account on GitHub.
curious_coder
curious_coderOP•13mo ago
I'm testing on branch 19
Brody
Brody•13mo ago
web scraper 😬
curious_coder
curious_coderOP•13mo ago
... yeah That's disjoint for the moment though... isn't it?
Brody
Brody•13mo ago
do you have permission to scrape what you're scraping?
curious_coder
curious_coderOP•13mo ago
Rookie error on my part... I imagine there's law surrounding what can and cannot be used from SpareRoom?
Brody
Brody•13mo ago
I don't know, I'm asking you lol, does spare room allow web scraping?
curious_coder
curious_coderOP•13mo ago
I've made the assumption that since it's publicly accessible data it can be taken. They've got not rate limiters but they also don't have an API. Honestly, I'm not sure really I imagine if they explicitly state "You're not allowed" then it's not allowed? Otherwise it's a grey area?
Brody
Brody•13mo ago
yes this is a grey area, running a scraper on railway is in their tos
curious_coder
curious_coderOP•13mo ago
(: Definitely a mistake then. If I get explicit permission, is that okay or still against tos? Failing that, I could use fictional data? I'd have to evidence that it's actually generated of course?
Brody
Brody•13mo ago
in the grand scheme of things, it's not that big of a deal
curious_coder
curious_coderOP•13mo ago
This project is moreso to help me learn the development process; idea to deployment, I don't mind too much if I don't use the data I scrape Also the first time I've started using Issue / Project in Github so it's all a learning curve tbh.
Brody
Brody•13mo ago
I don't usually like helping with scraping projects here in these forms, so I'll help, as long as you'll agree to find an api to provide this data scraping is unreliable anyway, it's for the better
curious_coder
curious_coderOP•13mo ago
Yeah, sure. When you say find an API; you generally mean an alternative data source (just because I'm not sure there's an API). Nevermind I found one 😄 https://developer.zoopla.co.uk/
Brody
Brody•13mo ago
if there's no api, you gotta ask yourself, is this a worthwhile project? I'm not saying your project is bad, I don't even know what spare room is, that's just my opinion on web scraping
curious_coder
curious_coderOP•13mo ago
Aha, I'm not sure really; it's more to showcase knowledge for my portfolio Ah, it's just a website people go to in order to rent properties
Brody
Brody•13mo ago
using an api is far more impressive than scraping
curious_coder
curious_coderOP•13mo ago
I'll throw it in then I imagine this means I'd (ideally) have to remove the SpareRoom specific scraping logic from the project in order for this to be hosted on Railway.app?
Brody
Brody•13mo ago
ideally yeah, but it's not like they actively look for people doing web scraping, what you're doing is small peanuts so either way, you've said you will use an api going forward, that's good with me your folder structure is a bit unorthodox if I wanted to run this project on my railway account would I need any kind of environment variables set?
curious_coder
curious_coderOP•13mo ago
Well there we are, I was trying to find the most orthodox folder structure for a flask app and this is one that I was given 😬 Nah, just the docker file specification Tbh though, I wonder if it would be worth moving it to root of the project
Brody
Brody•13mo ago
so you're saying I can fork this repo, slap in it a railway service and id get the same error you've shown?
curious_coder
curious_coderOP•13mo ago
Interestingly enough, I've put a little too much faith in AI to tell me the best directory structure for the project Yep
Brody
Brody•13mo ago
ewww ai
curious_coder
curious_coderOP•13mo ago
Should work out of the box In this instance, yeah 😂 I think it's good for basic and straightforward explanations but anything more and it craps itself
Brody
Brody•13mo ago
I've done a lot of help here, and most often than not ai just makes it worse
curious_coder
curious_coderOP•13mo ago
It's sent me down a rabbit hole or two Made things more complicated than they had to be
Brody
Brody•13mo ago
should i fork branch 19
curious_coder
curious_coderOP•13mo ago
Yeah That's the one I'm experimenting the deployment with Even trying to get better at version control 😂 although the commit history definitely shows where I tried and failed
Brody
Brody•13mo ago
these look like good commit messages to me where is index.html https://github.com/brody192/dwellist/blob/19-docker-deployment-issues/.gitignore#L4 remember when i asked this
curious_coder
curious_coderOP•13mo ago
Oh my god I am so sorry
Brody
Brody•13mo ago
🤣
curious_coder
curious_coderOP•13mo ago
I appreciate the help Brody, thank you. If it makes you feel like you've wasted time, I've learned the important lesson of not jumping the gun and specifically investigating to answer questions rather than assume. So, for me, not a waste of time 🙂
Brody
Brody•13mo ago
ive not wasted time 🙂 oh also, please use gunicorn, you are running flask in development mode
curious_coder
curious_coderOP•13mo ago
I had a crack at that but I can't copy that on windows as there's a dependency gunicorn uses that's not available for windows Although I suppose the point is to do the trial and error in the container
Brody
Brody•13mo ago
run the dev server locally, use gunicorn in your dockerfile
curious_coder
curious_coderOP•13mo ago
Ah okay 😄
Want results from more Discord servers?
Add your server