How can I get this to happen in Apache (with python, on Debian if it matters)?

User submits a form
Based on the form entries I calculate which html file to serve them (say 0101.html)
If 0101.html exists, redirect them directly to 0101.html
Otherwise, run a script to create 0101.html, then redirect them to it.

Thanks!
Edit: I see there was a vote to close as too broad (though no comment or suggestion). I am just looking for a minimum working example of the Apache configuration files I would need. If you want the concrete way I think it will be done, I think apache just needs to check if 0101.html exists, if so serve it, otherwise run cgi/myprogram.py with input argument 0101.html. Hope this helps. If not, please suggest how I can make it more specific. Thank you.
Apache shouldn't care. Just serve a program that looks for the file. If it finds it it will read it (or whatever and) return results and if it doesn't find it, it will create and return the result. All can be done with a simple python file.
Apache - if file does not exist, run script to create it, then serve it