Day 078 - Project 78 : What I've learned from 78 days of code blog site

If you have any questions, comments or issues with this project please post them here!

Hi!
While I have completed the challenge for Day78, for some reason the ‘style.css’ file is not being read/loaded (I don’t know the correct term) when I run the ‘main.py’ file. Can someone please help me out with this?
Here is the link to my Day78 Repl: https://replit.com/@Milindh/Day78100Days#main.py

from flask import Flask

contents = {}
for i in range(24):
  if ((i+77)%2==0):
    n = int(i+77)
    contents[n] = {"text": "Hmm. Looks super confusing. Not sure how this will work!"}
  else:
    n = int(i+77)
    contents[n] = {"text": "Hmm think I have got a hang of this. It is working now!"}

app = Flask(__name__)

@app.route('/')
def blankHome():
  page = ""
  return page

@app.route('/<pageNumber>')

def index(pageNumber):
  if (pageNumber == 'favicon.ico' or pageNumber == 'script.js' or pageNumber == 'style.css'): return ""
  tabTitle = f"Day {pageNumber} Reflection"
  heading1 = f"Reflection for Day {pageNumber}"
  heading2 = f"here is the code for Day {pageNumber}"
  n = int(pageNumber)
  text = contents[n]["text"]
  dayLink = f"https://replit.com/@SixftOne-MLH/Day{pageNumber}100Days#main.py"
  page = ""
  f = open("website.html", "r")
  page = f.read()
  f.close()
  page = page.replace("{tab title}", tabTitle)
  page = page.replace("{heading1}", heading1)
  page = page.replace("{heading2}", heading2)
  page = page.replace("{dayLink}", dayLink)
  page = page.replace("{text}", text)
  return page

app.run(host='0.0.0.0', port=81)

I used the above code. But it didn’t work. The ‘style.css’ file is not being read.

For more context: Note that the homepage is supposed to be blank. But if you will add any number at the end of homepage URL (for eg see this link: Day 78 Reflection) then a webpage will load up based on the number that you add. So based on the HTML code that I have written, the webpage that loads should read the ‘style.css’ file, but it doesn’t.

I did, it works when I tested it

For some reason its not working for me.

If I run the code and access this url : Day 78 Reflection - the background color of the website should be black and the font color should be white. But I get the default white background and black font color. Have attached a screenshot also.

because there are semicolons missing:
image

3 Likes

Nope the css file is still not being read. Did it work for you?

I still get the default style, see screenshot below:

Okay I found the solution.

Originally ‘style.css’ file was in the same/main directory right along with the ‘html’ file and hence the ‘css href’ in the html code was simply “style.css”

But if I create a ‘static’ folder and put the ‘style.css’ file in that folder, and then update the ‘css href’ in the html code to “static/style.css”, then it works!

:thinking: I don’t know the logic behind it, but the css file is being implemented now.

1 Like

that’s an interesting find! i reproduced this behavior by moving my css file out of the static folder. you can see what happens when you view the site source code and try to follow the link to the css file: you can’t access it because anything after the URL will be treated as the variable, file names included, unless there is another forwardslash. even then, apparently the static folder has to be named “static” or it’s inaccessible.

2 Likes

Question: Hi guys, I can’t seem to get past day 78. I’ve even looked at the solution to see where I’m going wrong, but I’m getting an internal server error

Repl link: https://replit.com/@lilywright93/Day78100Days#templates/reflection.html

from flask import Flask

app = Flask(__name__)

myReflections = {}

myReflections["75"] = {"link" : "https://replit.com/@lilywright93/Day75100Days", "reflection" : "Day 75"}

@app.route('/<pageNumber>')
def index(pageNumber):
  global myReflections
  page = ""
  f = open("templates/reflection.html", "r")
  page = f.read()
  f.close()

  page = page.replace("{day}", pageNumber)
  page = page.replace("{link}", myReflections[pageNumber]["link"])
  page = page.replace("{reflection}", myReflections[pageNumber]["Reflection"])
  return page

app.run(host='0.0.0.0', port=81)
<html>
  <head>
    <title>Day {day} Reflection</title>
  </head>
  <body>
    <h1>Day {day} Reflection</h1>
    <p><a href="{link}">Check out my code here</a></p>
    <p>{reflection}</p>
  </body>
</html>

Thanks in advance if anyone can help!

You’re getting a KeyError because on line 7 you used “reflection” but on line 19 you used “Reflection”

1 Like

You should end your program with:

if __name__ == __main__:
app.run(...)

No because that errors lol. __main__ is not defined and you didn’t indent. Either way it’s pretty useless for 100DoC. You never import the main.py file so there’s no reason for it.

1 Like

(I typed it out so I didn’t put indents)

from flask import Flask

app = Flask(__name__)

myReflections = {}

myReflections["78"] = {"link" : "https://replit.com/@ApoorvGoyal2/Day78100Days#main.py", "Reflection" : "Was a tough challenge"}


@app.route('/<pageNumber>')
def index(pageNumber):
  page = ""
  f = open("template/reflection.html", "r")
  page = f.read()
  f.close()
  page = page.replace("{day}", pageNumber)
  page = page.replace("{link}", myReflections[pageNumber]["date"])
  page = page.replace("{reflection}",myReflections[pageNumber]["date"])
  return page

app.run(host='0.0.0.0', port=81)

When I run this code, It says “Not Found”.

Yes, that is expected. Open it in a new tab and go to https://day78100days.apoorvgoyal2.repl.co/78

Python 100 days challenge Day 78 Flask webview

Repl link/Link to where the bug appears:

Hello, I am struggling to get to the webview link for my project. I am using the same logic as for Day 77 with a following link: https://day78100days.livalacaisse.repl.co/101 but this does not seem to work. I wonder what is the problem

code snippet. My main.py code:

from flask import Flask, request, render_template, redirect, url_for
import reflections_data

app = Flask(__name__, static_url_path='/static')

@app.route('/101', methods=['GET', 'POST'])
def home():
  if request.method == 'POST':
    day_number = request.form.get('day_number')
    name = request.form.get('name')
    reflection = request.form.get('reflection')
    url = request.form.get('url')

    # Store the reflection and URL
    reflections_data.reflections[day_number] = {
      'name': name,
      'reflection': reflection,
      'url': url
    }

    return redirect(url_for('reflection_page', pageNumber=day_number))

  return render_template('template/home.html')

@app.route('/<int:pageNumber>')
def reflection_page(pageNumber):
  pageNumber = str(pageNumber)
  reflection_info = reflections_data.reflections.get(pageNumber, {})

  return render_template('template/blog.html', 
    day_number=pageNumber, 
    name=reflection_info.get('name', 'Unknown'),
    title=f"Day {pageNumber} Reflections",
    text=reflection_info.get('reflection', 'No reflection yet.'),
    url=reflection_info.get('url', '#'))

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

The debug parameter in the app.run function can cause problems, so remove it. You should also specify host='0.0.0.0' in the app.run function, since it is the webview that listens to it.

3 Likes

Thank you KAlexK,

I have removed the debug parameter and specified the host, but still have the same problem… please let me know if any other ideas.

Here’s also interpret feedback - despite a removed debug parameter, it still says it is active:

  • Serving Flask app ‘main’
  • Debug mode: on
    WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
  • Running on http://127.0.0.1:5000
    Press CTRL+C to quit
  • Restarting with stat
  • Debugger is active!
  • Debugger PIN: 398-107-339

Updated code bellow:

from flask import Flask, request, render_template, redirect, url_for
import reflections_data

app = Flask(__name__, static_url_path='/static')

@app.route('/101', methods=['GET', 'POST'])
def home():
  if request.method == 'POST':
    day_number = request.form.get('day_number')
    name = request.form.get('name')
    reflection = request.form.get('reflection')
    url = request.form.get('url')

    # Store the reflection and URL
    reflections_data.reflections[day_number] = {
      'name': name,
      'reflection': reflection,
      'url': url
    }

    return redirect(url_for('reflection_page', pageNumber=day_number))

  return render_template('template/home.html')

@app.route('/<int:pageNumber>')
def reflection_page(pageNumber):
  pageNumber = str(pageNumber)
  reflection_info = reflections_data.reflections.get(pageNumber, {})

  return render_template('template/blog.html', 
    day_number=pageNumber, 
    name=reflection_info.get('name', 'Unknown'),
    title=f"Day {pageNumber} Reflections",
    text=reflection_info.get('reflection', 'No reflection yet.'),
    url=reflection_info.get('url', '#'))

if __name__ == '__main__':
  app.run(host='0.0.0.0')