неделя, 21 март 2021 г.

[FormBuilder]Survey programming with Flask and Bootstrap

 

Flask, Python

I have finally managed to complete this project and was surprised to find out there are not many tutorials on the web on how to program an online survey with Flask. So I guess this article might be useful or at least interesting to some Flask enthusiasts and professionals. The entire code of the project can be downloaded from here.

So, what is the core of the project? In fact, this is a very very simple and short survey displaying  the questions and their respective answer options from the questionnaire below:


 

Q1. What is your current age? - numeric field, 18+

Q2. What is your monthly income?

    1. Under 1000EUR
    2. 1001-1500EUR
    3. 1501-2000EUR
    4. 2001-2500EUR
    5. 2501-3000EUR
    6. 3001-3500EUR
    7. 3501-4000EUR
    8. 4001+ EUR
    99. Prefer not to answer

Q3. What activities do you like most?

    Hiking
    Airsoft
    Reading
    Computer gaming
    Board gaming
    Watching movies
    Other
    None of the above - screen out if selected

Q3.1 What other activities do you like? - if selected "Other" at Q3

Q3.2 On a scale 1 to 10, how would you rate the satisfaction of the activities you mentioned?

    Hiking
    Airsoft
    Reading
    Computer gaming
    Board gaming
    Watching movies
    Other

 There are a couple of key points that worth noting for this project.

Schema reading - here I have compiled the questionnaire as xml schema and I use the lxml module to extract all needed question details in order to use them in the survey. This is the moment where I made my first steps in OOP - most of my projects so far were written more or less procedurally and this is the first time I develop a class for my own idea, without following concrete tutorial. The class (QDetails) is located in lib.schema and contains one function - get_xml(). This function is extracting question text, question type, options texts and option ID numbers. I am planning to also add get_json() function with the purpose to extract the same details from json schema.

Database management - initially I planned to use MySQL for database engine, next I thought to move to PostgreSQL as the most modern SQL system nowadays but finally I ended with chosing SQLite. I think this is the most apropriate choice as this is lightweight and everything is in one file which is the best solution for autonomous survey projects. I use SQLiteStudio to visualize what happens in the table.

Data quality control - I have utilized quality control on two levels here:

- on-site data validation - I've developed a small library (located in lib.validate) which checks the data for each record right after it is collected in the backend but BEFORE being written to database. If collected data is invalid/incorrect, the functions in this library prevent incorrect data from writing to database.

- database data validation - I wrote a procedural checkscript (check_db.py) that can be run manuallty, separately from the Flask app. It's purpose is to verify the data stored in the database and if there is anything wrong Excel reports are generated in folder dv_report. I've based the script on pandas which is the most powerful Python data processing tool so far.

Here is how the entire thing works in action:


Link to the video >>.

What's next? As mentioned above I am planning to add a function that extracts question details from json. I'm also thinking about eventual expanding it one centralized app with GUI allowing to launch multiple survey, questionnaire building GUI exporting it's schema in xml and json and a couple of other functionalities.

1 коментар: