I want a flask server with 2 pages. first is fullscreen and realtime changing image.…
- User
I want a flask server with 2 pages. first is fullscreen and realtime changing image. Second is upload the image that would be set to first page.
- Assistant
I want a flask server with 2 pages. first is fullscreen and realtime changing image. Second is upload the image that would be set to first page.
To create a Flask server with two pages as described, where the first page displays a fullscreen, real-time changing image and the second page allows you to upload a new image, you can follow these steps:
Set up your project structure:
Create a directory for your project and organize it like this:
my_flask_app/
├── app.py
├── static/
│ └── uploaded_image.jpg
├── templates/
│ ├── display.html
│ └── upload.html
Install Flask:
Make sure you have Flask installed. You can install it using pip:
pip install flask
Create the Flask app (app.py):
This script will define your Flask application, handle routing, and process image uploads.
from flask import Flask, render_template, request, redirect, url_for
import os
app = Flask(__name__)
# Set the upload folder
UPLOAD_FOLDER = 'static'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def display_image():
return render_template('display.html')
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
if 'file' not in request.files:
return redirect(request.url)
file = request.files['file']
if file.filename == '':
return redirect(request.url)
if file and allowed_file(file.filename):
filename = 'uploaded_image.jpg'
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
return redirect(url_for('display_image'))
return render_template('upload.html')
if __name__ == '__main__':
app.run(debug=True)
Create the HTML templates:
display.html: This page displays the image full screen and refreshes it periodically.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Display Image</title>
<style>
html, body {
margin: 0;
height: 100%;
overflow: hidden;
background-color: black;
}
img {
display: block;
width: 100vw;
height: 100vh;
object-fit: contain;
}
</style>
</head>
<body>
<img src="{{ url_for('static', filename='uploaded_image.jpg') }}?{{ time }}" alt="Uploaded Image">
<script>
let interval = 3000; // Refresh every 3 seconds
setInterval(() => {
const img = document.querySelector('img');
img.src = `{{ url_for('static', filename='uploaded_image.jpg') }}?${new Date().getTime()}`;
}, interval);
</script>
</body>
</html>
upload.html: This page contains a form for uploading a new image.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Upload Image</title>
</head>
<body>
<h1>Upload a new image</h1>
<form action="{{ url_for('upload_file') }}" method="post" enctype="multipart/form-data">
<input type="file" name="file" accept="image/*">
<button type="submit">Upload</button>
</form>
</body>
</html>
Run the application:
Navigate to your project directory in the terminal and run the application:
python app.py
Accessing the application:
http://127.0.0.1:5000/ in your web browser to see the image. Initially, it will show the default uploaded_image.jpg.http://127.0.0.1:5000/upload to upload a new image file. Once uploaded, the image on the main page should automatically refresh to display the new image.This setup fulfills the requirements: it displays the image in fullscreen mode on the main page, updates automatically, and allows uploading new images via a secondary page.
Not yet included in a published release.