Black History Facts Generator: About The Web App

black history facts generator

I wrote a web app a couple of years back, at least, called Black History Facts Generator. You can visit it here. This post explains my inspiration behind the app and what language it’s written in.

My Inspiration For Black History Facts Generator

A few years back it was Black History Month and I was on Twitter providing random facts I knew. That was labor-intensive as I had to type out each fact and tweet it. I knew that had to be better way by creating a program to do this for me. That lead me to reading more about Twitter’s API and how I could connect a website to it to create tweets for me. Unfortunately, my programming skill was pretty poor then and I couldn’t figure out how to start. Thus, I gave up on that angle.

However, I got the idea to create a website displaying a random fact. My programming skills was good enough to do that. At the time I was teaching myself Python and I worked on the app while going through my coursework.

Eventually I was able to show the work my work through the website although the first version was rough. That version, which is no longer available to view, required a visitor to click a button to get a new fact. The current version shows random facts automatically every ten seconds by refreshing the page.

Programming Languages Used In The Site

I originally used Python and Flask in the web app and I still do till this day. You can view the code in my Github repo here. Or you can view the main bit of code tying everything together below:

@app.route("/", methods=["GET", "POST"])
def main():
    """
    This function opens and reads the bhfg_facts.txt file which is located in the Static (/static/) directory.
    Then a loop runs over the entire file, reading each line. The loop collects all the lines as a list, and then
    pulls out one fact randomly using the random.choice function.
    :return: The function returns a random Black History Fact to the home.html template, which displays it on the
    website.
    """

    with open("static/files/bhfg_facts.txt", "r") as facts:
        fact_list = facts.readlines()
    return render_template('index.html', black_history_fact=random.choice(fact_list))

However, I created a version using JavaScript and Node/Express/EJS when I taught myself JavaScript and Node/Express/EJS throughout 2021. That is the version you will get if you visit the domain today. Just like the Python/Flask version you can view the full code on its Github repo here. Again, I provide the main bit of code running the site below:

app.set("view engine", "ejs");

app.use(express.static("public"));

app.get("/", (req, res) => {
  var randomFact = facts.getFacts()[Math.floor(Math.random() * facts.getFacts().length)];

  res.render("index", {randomFact});
});

I use Heroku to deploy my web apps now after using PythonAnywhere for years. I recommend both for anyone wanting to deploy their web apps.

0 0 votes
Article Rating
Share My Post!
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback
1 month ago

[…] like my other web app, Black History Facts Generator, I created the app while teaching myself the Python programming language. The first version allow […]