Sure! I'll walk you through the step-by-step process to build a website using HTML, CSS, and Flask that helps you find craft beer near you.
Step 1: Setting Up the Project
Start by creating a new project folder. Open a command prompt/terminal in that folder and initialize a new project with Flask by running the following command:
$ pip install flask
$ flask --version
Step 2: HTML Markup
Create a new file named index.html inside the project folder. This file will contain the HTML markup for our website. Here's a basic example of what it might contain:
<!DOCTYPE html>
<html>
<head>
<title>Finding Craft Beer Near Me</title>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='styles.css') }}">
</head>
<body>
<div class="container">
<h1>Finding Craft Beer Near Me</h1>
<form action="{{ url_for('find_beer') }}" method="POST">
<input type="text" name="location" placeholder="Enter your location">
<button type="submit">Find Beer</button>
</form>
<ul id="results"></ul>
</div>
<script src="{{ url_for('static', filename='script.js') }}"></script>
</body>
</html>
Don't worry about the Flask specific code ({{ url_for(...) }}), we will set it up shortly.
Step 3: Styling with CSS
Create a new file named styles.css inside a folder named static in the project folder. This file will contain the CSS styling for our website. Here's a sample CSS snippet:
body {
font-family: Arial, sans-serif;
background-color: #f5f5f5;
margin: 0;
padding: 0;
}
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
margin-top: 0;
}
form {
display: flex;
justify-content: center;
margin-bottom: 20px;
}
input[type="text"] {
padding: 10px;
width: 300px;
border: 1px solid #ccc;
border-radius: 3px;
outline: none;
}
button {
padding: 10px 20px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 3px;
cursor: pointer;
outline: none;
}
ul {
list-style-type: none;
padding: 0;
}
li {
margin-bottom: 10px;
padding: 10px;
background-color: #f5f5f5;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
border-radius: 3px;
}
Step 4: Flask Backend
Create a new Python file named app.py in the project folder. This file will handle the backend Flask code. Here's an example implementation:
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/find_beer', methods=['POST'])
def find_beer():
location = request.form['location']
# Process the location and retrieve craft beer results using an API (e.g., Google Maps API)
# Modify and return the result as per your requirement (e.g., JSON, HTML, etc.)
results = []
# Example: results = find_craft_beer_nearby(location)
return render_template('index.html', results=results)
if __name__ == '__main__':
app.run()
This code sets up a Flask application, defines two routes (/ and /find_beer), and renders the HTML template (index.html). Modify the 'find_beer' route to fit your API requirements or the services you want to integrate.
Step 5: JavaScript Logic
Create a new file named script.js inside the static folder. This file will contain the JavaScript logic to handle the form submission and display the craft beer results. Here's a starting point:
document.addEventListener('DOMContentLoaded', () => {
const form = document.querySelector('form');
const results = document.getElementById('results');
form.addEventListener('submit', (e) => {
e.preventDefault();
const location = form.querySelector('input[name="location"]').value;
results.innerHTML = 'Searching...';
// Make an AJAX request to the Flask backend for the '/find_beer' route
fetch('/find_beer', {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: `location=${location}`
})
.then(response => response.json())
.then(data => {
results.innerHTML = ''; // Clear previous results
data.forEach(item => {
const li = document.createElement('li');
li.textContent = item.name;
results.appendChild(li);
});
})
.catch(error => {
results.innerHTML = 'An error occurred while fetching the data.';
console.error(error);
});
});
});
This code listens for the form submission event, retrieves the location entered by the user, makes an AJAX request to the Flask backend, and appends the results as list items (<li>) to the unordered list (<ul id="results">).
Step 6: Run the Application
Save all the files and run the Flask application by executing python app.py in the command prompt/terminal. It should start a local server at http://127.0.0.1:5000/.
Visit that URL in your web browser, and you should be able to see and use the "Finding Craft Beer Near Me" website. Enter a location, click "Find Beer," and it will fetch and display the craft beer results.
Remember to handle the backend logic for searching craft beer near the provided location using appropriate APIs or services as per your requirements.