Forums

"Head First Python" first webapp issue

I'm following along with exercises in the book "Head First Python". I've created an app that works locally, but it isn't working on PythonAnywhere. The page https://shadelovingtropes.pythonanywhere.com/swimmers should have a populated drop down menu, but it doesn't seem to be picking up the data. I confirmed all data files were uploaded. Any ideas on things I can check? I think this is the relevant code. Thanks

if "swimmers" not in session:
    swim_files = os.listdir(swimclub.FOLDER)
    swim_files.remove(".DS_Store")
    session["swimmers"] = {}
    for file in swim_files:
        name, *_ = swimclub.read_swim_data(file)
        if name not in session["swimmers"]:
            session["swimmers"] = []
        session["swimmers"][name].append(file)

@app.get("/swimmers") def display_swimmers(): populate_data() return render_template( "select.html", title="Select a swimmer", url="/showfiles", select_id="swimmer", data=sorted(session["swimmers"]), )

There are a few things that could be causing that:

  1. you do not have any files in the directory that is pointed to by swimclub.FOLDER
  2. the directory does not exist - be aware of your working directory when checking this: https://help.pythonanywhere.com/pages/NoSuchFileOrDirectory/

Also, if one of those was true in the past but are no longer true, the if swimmers not in session: would ensure that, once you've populated the session variable with an empty dictionary, it would not be re-populated with new data.