Thoughts on programming, web development and design.

Circuit Board

Category: Web Development Page 1 of 31

Creating a simple weather app with Open Weather API

Updated on January 31, 2025

How do you get the weather? You could use an app or create your own. First, you need a way to get weather data. You can use an Application Programming Interface to get the data that you need.

Creating a simple weather app with Open Weather API

Where can you find APIs to use in your projects? Try ProgrammableWeb. It is an API directory that lets you find the right API for your project. I found a weather API called Open Weather. Open Weather allows you to get the current weather data, hourly or daily forecast and more.

With Open Weather, I created a simple weather app. It gets the current temperature, daily forecast (highs and lows) and weather conditions. Then, it tells you if the weather was right for riding a bike.

Getting Started

Before writing any code, I looked at Open Weather’s documentation. It explains how to use their API. They include examples using different programming languages like JavaScript. These examples are helpful were helpful to learn what I could do with the API.

On their examples’ page, I found Weather.js. Weather.js fetches data from Open Weather for you. It makes it easy to get weather information from Open Weather.

Building the App

Before building the app, I researched other weather apps to get an idea of what I wanted mine to look like. Then, I sketched out an idea on paper.

I chose to use HTML, CSS and JavaScript. Since I am familiar with Bootstrap, I used it as well. I built my prototype with Bootstrap’s starter template. Then, I wrote my own JavaScript file to fetch data from Open Weather using Weather.js.

Open Weather has weather icons. Weather.js doesn’t use those icons. I looked at the JavaScript and wrote code to get the icons.


Weather.Current.prototype.icon = function () {
  return this.data.list[0].weather[0].icon;
}

Now, my app shows the current and forecast temperatures, weather icon and conditions.

Bike Weather App Screenshot

Why update?

The version of Bootstrap was old. I wanted to add a feature to get the visitor’s location and display where they are from. It would get the weather forecast for their location. Instead of getting the weather for Milwaukee, WI.

Changes to Bootstrap

The previous version had a feature called Jumbotron. Bootstrap 5 has dropped this component. You can use Bootstrap’s utilities to recreate the look. The layout is a bit different from the original.

Bike Weather Banner

To display the forecast, I used Bootstrap cards. Then, I used FlexBox to make the cards the same height. Bootstrap has a helper to create a sticky footer.

Reverse Geocoding

One option to get your local weather is to use the City and State. In the first version, I hardcoded it. Open Weather has another API for getting your browser’s location. You can do a reverse geocode lookup. HTML has a utility for getting the users location.


function getLocation() {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function(position){
          const longitude = position.coords.longitude;
          const latitude = position.coords.latitude;
          const geoURL = "http://api.openweathermap.org/geo/1.0/reverse?lat=" + latitude + "&lon=" + longitude + "&limit=1&appid=" + Weather.APIKEY;
  
          fetch(geoURL)
            .then(response => response.json())
            .then(data => {
              let cityName = data.map(a => a.name);
              let state = data.map(a => a.state);
              document.getElementById("location").innerHTML = cityName + ", " + state;
              console.log(data);
            })
            .catch(error => {
              console.error('Error fetching data:', error);
              document.getElementById("location").innerHTML = "Milwaukee, Wisconsin";
            });
        })
    } else {
        document.getElementById("location").innerHTML = "Milwaukee, Wisconsin";
    }
  }

By getting the location and doing a reverse lookup, I was able to modify the code. It now gets the forecast based on the user’s location. In web development, updating the code to a new version helps you to develop better solutions. Sometimes updating the CSS framework helps you to build a better user experience.

Restart Numbered List with CSS

Normally, you can renumber an HTML ordered list. You use the attribute “start” to set the value you want to restart the numbers at. What happens when you have a special style that overrides how the numbers are generated?

Restart Number Lists with CSS

Image by Alicja from Pixabay

For example, I use the following CSS style to override how a list looks.

ol.simple-list {
	counter-reset: li-counter;
	list-style-type: none;
	list-style-type: decimal !ie;
	margin: 0;
	margin-left: 3em;
	padding: 0;
}


It creates a list with numbers like this.

Green Numbered List

When I tried to use the start attribute to restart numbering a list, it set all of the numbers to 1. I tried to update the style with newer CSS styling. It didn’t fix the problem. Finally, I found the right way to make this work. The attribute “counter-increment” lets you reset the counter. You have to pick a number one less than what you want to start at.

.simple-list-counter-9 {
	counter-increment: li-counter 9;
}

By doing this, you can restart your list at the desired number. With the above style the new list starts at 10 instead of 1. This works if all your lists end at number 9. If you want to restart at a different style, I suggest using an in-line style instead of adding it to your CSS file.

Naming Colors in Your CSS

Naming things in programming in hard. What you name things matters. Naming is communication. You want the names in your CSS to be easy to understand. They need to quickly communicate what you were thinking.

What naming convention do you use to name your colors in CSS? CSS can be difficult to maintain. Save yourself and others time by picking an easy naming convention. Your choice can save you hours of debugging in the future.

Naming Your Colors IN CSS

Image by Myriams-Fotos from Pixabay

Choose a Naming Convention

There are two types of naming or variable categories in CSS. Descriptive or functional. A descriptive name describes the value. A functional name describes a use. If you want a variable to display a blue color, you might name it --color-blue. When you want a variable for a specific purpose like an accent color, you might name it --color-accent. Use both variable categories to make it easier to maintain your custom color palette.

Naming Colors

Colors have tints, shades and tones. How do you know what to call each one? Use The Color API to find out what to call the colors in your color palette. You can put in a hex code or RGB value to identify your color.

Start with your color palette

It’s good practice to limit the number of colors in your design. Use descriptive names to define your color palette. Then, use functional names to further define your styles.

root: {
brand-blue-color: #0000ff;  
brand-green-color: #008000;
}

.article-title {
color: var(--brand-blue-color);
}

.primary-button {
color: var(--brand-color-green);
}

When you need to change your colors, you can easily do it. Simply change the color code. If that won’t work, create a new descriptive name and use that instead. Whatever strategy you choose, make it simple so you or someone else can easily make updates in the future.

Make Your Own UI Pattern Library

What are User Interface Design Patterns? A pattern is a plan or model for making something. In software development, patterns provide solutions to specific design problems. Buttons tell people to do something. Download, Buy, Continue or Save.

Make Your Own UI Library

Photo by Susan Q Yin on Unsplash

When you design a website or web app, you’ll need a navigation bar, button, tabs, forms and more. Common design patterns help to make your design easy to use and user friendly.

What is a UI Pattern Library?

A UI Pattern Library is a collection of user interface components or patterns. It helps you to define how each component looks and works. Your pattern library helps to ensure a consistent look and feel across your products. You want to add solutions so you don’t have to solve the same design problem over and over again. Or search your app for the pattern that you need to use.

What goes in it?

You’ll want to define your user interface. How you create buttons, navigation bars, layouts, alerts and notifications. Include guidelines on how and when to use them. You may want to include code snippets so you don’t have to rebuild a component from scratch. Also include color palettes, typography and grid layouts.

Where to find Design Pattern Libraries and Resources?

Build Your Own

When you build your own pattern library, get designers and developers involved. A pattern library is a collaborative project. Both your designers and developers must work together in creating and maintaining it.

  • Decide on what to name things The team needs to decide what to call a component. A shared naming convention prevents communication problems.
  • Use a ticket system to track updates Your design library has to be updated and maintained. A ticket system makes it easy to track changes. You can create an approval system for requests.
  • Audit your library Your library can quickly become out of date. Remove old components, templates and no longer used patterns. Plan to audit your library at least once a year.

Switching to Utility-First CSS

When you write CSS, do you create classes that reflect the UI? Classes like card, button, title, sidebar or link. Your classes have every possible element defined in that class.

Switching to Utility-First CSS

Image by superdirk from Pixabay

What if you didn’t write your CSS that way? Utility-first CSS creates a series of helper classes or utilities. A utility class does one thing really well. For example, you might write a class that changes the background color to blue. Then, you would use to change the background color on buttons, cards and CTA boxes. Use it wherever you want. You mix and match these utilities to create the look you want.

Where can you learn more about it?

Start by checking out these articles:

What CSS Methodologies can I use?

You can look at these couple popular methodologies:

  • Cube CSS – a CSS methodology that focuses on simplicity, consistency and works with whatever medium you are using.
  • Tailwind CSS – a CSS framework that helps you rapidly build modern websites.
  • Atomizer – a CSS utility library that integrates with the most popular web frameworks.

Software development consistently changes. A developer needs to experiment with new tools and new ideas. You need to pick the tools that best suit your project and users.

Page 1 of 31

Powered by WordPress & Theme by Anders Norén