import asyncio from pyppeteer import launch import os from PIL import Image from flask import Flask, send_file import io app = Flask(__name__) async def render_html_to_png(html_file_name, css_file_name, output_file_name): #browser = await launch() browser = await launch(executablePath='/usr/bin/google-chrome-stable', headless=True, args=['--no-sandbox']) page = await browser.newPage() await page.setViewport({'width': 800, 'height': 480}) current_dir = os.path.dirname(__file__) html_file_path = os.path.join(current_dir, html_file_name) css_file_path = os.path.join(current_dir, css_file_name) output_file_path = os.path.join(current_dir, output_file_name) await page.goto(f'file://{html_file_path}') await page.addStyleTag({'path': css_file_path}) screenshot = await page.screenshot() image = Image.open(io.BytesIO(screenshot)) image.save(output_file_path) await browser.close() @app.route('/weather', methods=['GET']) def generate_weather_image(): html_file_path = 'weather.html' css_file_path = 'styles.css' output_file_path = 'weather.png' asyncio.get_event_loop().run_until_complete(render_html_to_png(html_file_path, css_file_path, output_file_path)) return send_file(output_file_path, mimetype='image/png') if __name__ == "__main__": app.run(host='0.0.0.0', port=5233)