Forums

Forms in stages with Flask/Python

Comparative flask noob here, with what might be a basic and silly question! I'm doing well with fairly simple Flask applications, e.g. of the type where we have n inputs: (Input_1,Input_2,....,Input_n), which our Python back-end grabs from our web form via request.form[ ] and feeds into some algorithm (Process_A), the outputs of which (Output_1.......Output_m) are displayed on the same web page via render_template('template.html',.......) I want to do something a little more complex. To summarize:

  1. Enter Input_1 and Input_2 in html form
  2. Send to Python code using request.form
  3. In Python back-end, apply Operation A to inputs to generate Output 1
  4. Send Output 1 back to the html front-end using render_template and display it
  5. Have a new form field appear, in which the user can enter Input_3
  6. Input_3, plus any (or all, or none) of Inputs 1 and 2 and Output 1 sent to Python via request.form
  7. Operation B takes these and produces further outputs, which are then sent back to the front-end. etc etc etc.

In essence, I want a form that performs calculations in stages, allowing the user to base subsequent inputs on the results of earlier operations. This is obviously super easy in Python with the console or data files or whatever - it's implementing it as an online calculator app that I'm stuck on. Particular questions:

  • Do I need a new flask route for each subsequent passing back-and-forth of data?
  • What should the arguments of these route definitions be?
  • Do previous input definitions persist through the single piece of Python code? i.e. if Inputs 1 and 2 and Output 1 are to be arguments for Operation B, do they need to be re-requested in a new route function, or can they simply be used as is?
  • How do I handle the 'staged reveal' of the form at the html end?

Any and all ideas warmly appreciated! Cheers!

We on the tech support team here can only really provide answers here about coding on PythonAnywhere specifically (though perhaps another user of the forum might have some thoughts). However, you might find our blog post on turning a Python script into a website of interest -- the last example does pretty much what you describe.

Giles - Many thanks, that's very helpful indeed. :)

Great! Glad you found it useful :-)