views:

24

answers:

1

I want to write a web-based code generator for a Python crawler. Its aim is to automatically generate code so a developer doesn't need to write it, but I've run into this problem: in one of my project's webpages, there are some checkboxes, buttons, etc. Each of them generates some Python code and writes it to a common textarea. However, when I uncheck boxes I can't figure out how to remove the corresponding code from the textarea, because it's all been mixed together. For example:

  • Check box 1 -- it writes code snippet 1 to the textarea
  • Check box 2 -- it writes code snippet 2 to the textarea
  • Check box 3 -- it writes code snippet 3 to the textarea
  • Uncheck box 2 -- it needs to somehow remove code snippet 2 from the text area, but leave behind code snippets 1 and 3

Is there some way to fix this problem, or any better solution for the whole code generator project?

A: 

You need to separate out the idea of what code to generate from the events triggering generation.

What code is generated is governed by the combined set of all the checkboxes that are checked.

Triggering code generation occurs each time any of them are changed. You need to regenerate everything at that time.

In detail:

  • Check box 1 -- triggers generation, just code for snippet 1 included
  • Check box 2 -- triggers generation, code for snippets 1 & 2 included
  • Check box 3 -- triggers generation, code for snippets 1, 2 & 3 included
  • Uncheck box 2 -- triggers generation, code for snippets 1 & 3 included
Andy Dent