Flask Web Deployment on Google Cloud
Follow the project here Convert Schedule to .ics, we will going to edit the app.py
(main.py
)
source code here
Python Script Development
Here is a little bit different from regular app. Most of time we have a landing page under root (/)
but since my script is a POST
method which is a callable page. It cannot be used to independently. Thus we choose to load the index.html
first at root page.
Your main.py
file is set up to deploy a web application using Flask, a popular micro web framework in Python. Let's break down this file step by step:
Importing Libraries and Modules
from flask import Flask, request, send_file
from flask_cors import CORS
import io
import datetime
import cal_web # Import your cal_web script
Flask
: The main class in Flask; it is used to create your application.request
: To handle incoming requests to your server.send_file
: A Flask utility to send files as responses.CORS
: A Flask extension that handles Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible.io
: For in-memory file operations.datetime
: For handling date and time.cal_web
: This is your script that contains the logic for parsing input and creating calendar files.
Setting Up the Flask App
app = Flask(__name__)
CORS(app)
- Initializes a new Flask application.
- Enables CORS for this application.
Routing
Index Route
@app.route('/')
def index():
return app.send_static_file('index.html')
- The decorator
@app.route('/')
tells Flask to execute this function when the root URL is accessed. - Returns the
index.html
file. This is likely the entry point of your web application's frontend.
Generate ICS Route
@app.route('/generate-ics', methods=['POST'])
def generate_ics():
...
- This route handles the generation of
.ics
files. - It's set to respond to POST requests to
/generate-ics
.
Handling POST Request to Generate ICS
-
Extracting Data from Request:
data = request.json
gets the JSON data sent with the POST request.- Extracts
startDate
,endDate
, andschedule
from the JSON data.
-
Converting Dates:
- Converts
startDate
andendDate
from string todatetime
objects. - If the conversion fails, it returns a 400 error with an "Invalid date input" message.
- Converts
-
Generating ICS File:
- Uses
cal_web.parse_input
to parse the schedule text. cal_web.parse_and_create_ics_files
is then used to create a ZIP file containing the.ics
files.
- Uses
-
Sending the ZIP File as a Response:
- The ZIP data is converted to a file-like object using
io.BytesIO
. send_file
is used to send this file as a response to the client, with appropriate headers to prompt a file download.
- The ZIP data is converted to a file-like object using
Running the App
if __name__ == '__main__':
app.run(debug=True)
- This condition ensures that the server is run only if this script is executed directly (not imported as a module).
app.run(debug=True)
starts the Flask application with debug mode enabled. Debug mode provides a reloader and debugger.
Summary
In summary, this script sets up a Flask web server with two endpoints: one for serving the index.html
file and another for handling POST requests to generate .ics
files from a given schedule. The script integrates with the cal_web
module you've previously written to handle the calendar-related logic. The use of CORS
allows for cross-origin requests, making it suitable for a modern web application setup.
.yaml edit
Usually editing in this way
runtime: python39 # Specify the Python version
entrypoint: gunicorn -b :$PORT main:app # Replace 'main' with the name of your Python file without '.py'
Next use html Script HTML Template