diff --git a/projects/view-weather-with-html-css-js/finished_weather_app.gif b/projects/view-weather-with-html-css-js/finished_weather_app.gif new file mode 100644 index 00000000..959667f1 Binary files /dev/null and b/projects/view-weather-with-html-css-js/finished_weather_app.gif differ diff --git a/projects/view-weather-with-html-css-js/header.png b/projects/view-weather-with-html-css-js/header.png new file mode 100644 index 00000000..5eae28e5 Binary files /dev/null and b/projects/view-weather-with-html-css-js/header.png differ diff --git a/projects/view-weather-with-html-css-js/new_open_weather_account_form.png b/projects/view-weather-with-html-css-js/new_open_weather_account_form.png new file mode 100644 index 00000000..41973b6a Binary files /dev/null and b/projects/view-weather-with-html-css-js/new_open_weather_account_form.png differ diff --git a/projects/view-weather-with-html-css-js/open_weather_account_prompt.png b/projects/view-weather-with-html-css-js/open_weather_account_prompt.png new file mode 100644 index 00000000..ca60d80e Binary files /dev/null and b/projects/view-weather-with-html-css-js/open_weather_account_prompt.png differ diff --git a/projects/view-weather-with-html-css-js/open_weather_api_key.gif b/projects/view-weather-with-html-css-js/open_weather_api_key.gif new file mode 100644 index 00000000..f9b394cf Binary files /dev/null and b/projects/view-weather-with-html-css-js/open_weather_api_key.gif differ diff --git a/projects/view-weather-with-html-css-js/rendered_weather_app_html_only.png b/projects/view-weather-with-html-css-js/rendered_weather_app_html_only.png new file mode 100644 index 00000000..28883762 Binary files /dev/null and b/projects/view-weather-with-html-css-js/rendered_weather_app_html_only.png differ diff --git a/projects/view-weather-with-html-css-js/rendered_weather_app_with_css_1.png b/projects/view-weather-with-html-css-js/rendered_weather_app_with_css_1.png new file mode 100644 index 00000000..33a4d654 Binary files /dev/null and b/projects/view-weather-with-html-css-js/rendered_weather_app_with_css_1.png differ diff --git a/projects/view-weather-with-html-css-js/rendered_weather_app_with_css_2.png b/projects/view-weather-with-html-css-js/rendered_weather_app_with_css_2.png new file mode 100644 index 00000000..4c9302c6 Binary files /dev/null and b/projects/view-weather-with-html-css-js/rendered_weather_app_with_css_2.png differ diff --git a/projects/view-weather-with-html-css-js/rendered_weather_app_with_css_3.png b/projects/view-weather-with-html-css-js/rendered_weather_app_with_css_3.png new file mode 100644 index 00000000..cb20f264 Binary files /dev/null and b/projects/view-weather-with-html-css-js/rendered_weather_app_with_css_3.png differ diff --git a/projects/view-weather-with-html-css-js/rendered_weather_app_with_css_4.png b/projects/view-weather-with-html-css-js/rendered_weather_app_with_css_4.png new file mode 100644 index 00000000..b561ca1a Binary files /dev/null and b/projects/view-weather-with-html-css-js/rendered_weather_app_with_css_4.png differ diff --git a/projects/view-weather-with-html-css-js/view-weather-with-html-css-js.mdx b/projects/view-weather-with-html-css-js/view-weather-with-html-css-js.mdx new file mode 100644 index 00000000..4159f5e2 --- /dev/null +++ b/projects/view-weather-with-html-css-js/view-weather-with-html-css-js.mdx @@ -0,0 +1,607 @@ +--- +title: View the Weather With HTML, CSS, & JavaScript +author: Brandon Dusch +uid: H7lTRTjDJ0VbnaVUJdWSclFxwiH3 +datePublished: 2023-07-21 +description: Learn to build a weather app with HTML, CSS, & JavaScript. +header: https://raw.githubusercontent.com/codedex-io/projects/main/projects/view-weather-with-html-css-js/header.png +tags: + - intermediate + - javascript +--- + + + +# Build a Chat App With Socket.IO + + + + + +**Prerequisites:** HTML/CSS, JavaScript +**Versions:** HTML5, CSS3, ES6+ (JavaScript) +**Read Time:** 60 minutes + +## Introduction + +Have you ever wondered how some apps and sites let you check the weather? + +In this tutorial, we're going to learn to build a web app that displays the current weather, using HTML, CSS, JavaScript, and a special ingredient... APIs! + +With the power of [Open Weather](https://openweathermap.org/) and a bit of coding magic, we can check the weather by location. + +By the end of this tutorial, you will have learned to utilize the following: + +- Fetching asynchronous data with API secrets/keys. +- Organizing content with a CSS layout model. +- Changing what's on the rendered page. + +The finished weather will look and work like this: + +Finished Weather Application + +Let's get started! + +## Step 1: Project Setup + +First, we'll need to create the folders and files that will hold the code for our project. Let's begin by opening a terminal and creating a new folder named **weather-app** using the `mkdir` command. Then, go into this folder by using the `cd` command: + +``` +mkdir weather-app +cd weather-app +touch index.html styles.css index.js +``` + +This will create the following files in the **weather-app** folder: + +- An **index.html** file to render the application. +- A **styles.css** file for customizing the appearance of our application. +- An **index.js** file where we will fetch our weather data. + +We will return to these files later on. But for now, let's head over to OpenWeather! + +## Step 2: Get API Key From OpenWeather + +OpenWeather provides several APIs (or application programming interfaces) that feature interesting weather-related data. + +If you haven't already, you must create an OpenWeather account with a username, email, and password: + +Form for new account with OpenWeather + +**Note:** You'll be sent a verification email; make sure to answer it! + +Next, you'll be prompted to state what you want to do with OpenWeather's API. + +Prompt by OpenWeather (post-login) + +I recommend choosing “Education/Science" or “Other". + +You should now be ready to view your API key! After creating a new account, a new key should be generated for you. Near the top right, select your username, followed by “My API keys" in the dropdown: + +Screen for generating new keys for OpenWeather API + +After confirming you have an active API key, let's move on to the next step! + +## Step 3: Write HTML + +It's time to start writing some code! + +Let's return to our code editor and re-open the **index.html** file. + +Add the following HTML to start: + +```html + + + + + Weather App + + + + + +``` + +Aside from the necessary `` declaration, we've included a `` element in the `` to connect our HTML with our **styles.css** file (which we'll work on in the next step). + +Next, let's add the following HTML inside the `` element: + +```html + +
+
+

Weather App

+ + +
+
+ + +``` + +The `
` element will hold all of our app structure, which includes a heading followed by two `
` elements: + +- One contains `` elements that include an event listener, which activates a function when something happens to the element, such as being clicked (i.e., `onclick="fetchWeather()"`). +- The other one is empty for now, but will eventually be populated with weather data when we write the `fetchWeather()` event listener later on in this tutorial. + +We also added a ` + + \ No newline at end of file diff --git a/projects/view-weather-with-html-css-js/weather-app/script.js b/projects/view-weather-with-html-css-js/weather-app/script.js new file mode 100644 index 00000000..249700e6 --- /dev/null +++ b/projects/view-weather-with-html-css-js/weather-app/script.js @@ -0,0 +1,69 @@ +async function fetchWeather() { + // Step a. Create global variables and start inner functions + let searchInput = document.getElementById('search').value; + const weatherDataSection = document.getElementById("weather-data"); + weatherDataSection.style.display = "block"; + + const apiKey = '26571981230f1dd6b72e6b2c6548535f' + // const apiKey = "REPLACE WITH YOUR API KEY" + + + if(searchInput == "") { + weatherDataSection.innerHTML = ` +
+

Empty Input!

+

Please try again with a valid city name.

+
+ `; + return; + } + + // Step b. Get lat and lon coordinates via Geocoding API + async function getLonAndLat() { + const countryCode = 1 + geocodeURL = `http://api.openweathermap.org/geo/1.0/direct?q=${searchInput.replace(" ", "%20")},${countryCode}&limit=1&appid=${apiKey}` + + const response = await fetch(geocodeURL); + if(!response.ok) { + console.log("Bad response! ", response.status); + return; + } + + const data = await response.json(); + if(data.length == 0) { + console.log("Something went wrong here."); + weatherDataSection.innerHTML = ` +
+

Invalid Input: "${searchInput}"

+

Please try again with a valid city name.

+
+ `; + return; + } else { + return data[0]; + } + } + + async function getWeatherData(lon, lat) { + // Step c. Get weather information via Current Weather API + const weatherURL = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${apiKey}` + const response = await fetch(weatherURL); + + // Step d. Display the weather data + const data = await response.json(); + weatherDataSection.style.display = "flex"; + weatherDataSection.innerHTML = ` + ${data.weather[0].description} +
+

${data.name}

+

Temperature: ${Math.round(data.main.temp - 273.15)}°C

+

Description: ${data.weather[0].description}

+
+ ` + } + + // These are part of Step d. + document.getElementById("search").value = ""; + const geocodeData = await getLonAndLat(); + getWeatherData(geocodeData.lon, geocodeData.lat); +} diff --git a/projects/view-weather-with-html-css-js/weather-app/styles.css b/projects/view-weather-with-html-css-js/weather-app/styles.css new file mode 100644 index 00000000..354f827c --- /dev/null +++ b/projects/view-weather-with-html-css-js/weather-app/styles.css @@ -0,0 +1,82 @@ +* { + margin: 0; + padding: 0; + box-sizing: border-box; +} + + +body { + background-image: url(https://www.travellens.co/content/images/size/w2000/format/webp/2022/06/Dumbo-NYC.jpg); + background-repeat: no-repeat; + background-size: cover; + font-family:'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif; + height: 100vh; +} + + +main { + background-color: rgba(227, 193, 173, 0.85); + position: relative; + top: 30%; + border: 1px solid; + border-radius: 5px; + width: 45%; + margin: auto; + padding: 2em; +} + + +h1 { + margin-bottom: 20px; +} + + +#search { + border-radius: 5px 0 0 5px; + border: none; + padding: 10px; + font-size: 16px; + width: 70%; + height: 48px; +} + + +#submit { + border-radius: 0 5px 5px 0; + padding: 10px; + font-size: 16px; + width: 5em; + cursor: pointer; +} + +#weather-wrapper { + display: flex; + flex-direction: column; + place-items: center; + height: 250px; +} + +#weather-search { + display: flex; + width: 50%; + gap: 5px; +} + + +/* #weather-data { + background-color: rgba(255, 255, 255, 0.85); + border-radius: 5px; + padding: 1.5em; + margin-top: 20px; + + + text-align: center; + align-items: center; + gap: 12px; +} + + +#weather-data > img { + border-radius: 50%; + background-color: lightskyblue; +}*/