Browse Source

improved_v0

Francesco 1 year ago
parent
commit
256ea2e193

BIN
Techman 4.doc


+ 10 - 1
flask_be/app.py

@@ -1,11 +1,15 @@
 from flask import Flask, request
 from flask_cors import CORS
 
-from engine.simple_query import doSimpleQuery
+from engine.query_generator import doSimpleQuery
 
 app = Flask(__name__)
 CORS(app) # This will enable CORS for all routes
+# CORS enabling is useful to prevent annoying CORS error when developing locally
 
+
+################################################################
+# parte di codice da copiare per fare un altro endpoint in Flask
 @app.route('/simple_get_query', methods=['POST'])
 def simpleQuery():
 
@@ -13,6 +17,8 @@ def simpleQuery():
 
     try:        
         data = request.form['word']
+
+        # Il motore: classe funzionale, che sta nel pacchetto 'engine'
         output = doSimpleQuery(data, appath)
 
         return output, 200
@@ -20,6 +26,9 @@ def simpleQuery():
     except:
         emptyOut = {}
         return emptyOut, 500
+# fino a qui
+#################################################################
+
 
 if __name__ == '__main__':
     app.run()

BIN
flask_be/engine/__pycache__/query_generator.cpython-310.pyc


BIN
flask_be/engine/__pycache__/simple_query.cpython-310.pyc


+ 0 - 0
flask_be/engine/data_interface_sqlite3/__init__.py


BIN
flask_be/engine/data_interface_sqlite3/__pycache__/__init__.cpython-310.pyc


BIN
flask_be/engine/data_interface_sqlite3/__pycache__/query_handlers.cpython-310.pyc


+ 1 - 1
flask_be/engine/simple_query.py → flask_be/engine/data_interface_sqlite3/query_handlers.py

@@ -1,6 +1,6 @@
 import sqlite3
 
-def doSimpleQuery(data, path):
+def simpleQueryHandler(data, path):
 
     theSimpleQuery = "SELECT norm FROM form WHERE norm LIKE '" + data + "'"
 

+ 7 - 0
flask_be/engine/query_generator.py

@@ -0,0 +1,7 @@
+from engine.data_interface_sqlite3.query_handlers import simpleQueryHandler
+
+def doSimpleQuery(data, path):
+
+    results = simpleQueryHandler(data, path)
+
+    return results

+ 3 - 0
flask_be/engine/simple_query_test.py

@@ -1,3 +1,6 @@
+# Questo NON è parte del codice: è un notebook Jupyter (nell'implementazione di VSCode)
+# che ho usato per fare dei test!
+
 # %%
 # Test code using Jupyter
 

+ 21 - 0
readme.md

@@ -0,0 +1,21 @@
+To get flask running (use Python 3!):
+
+1. [Optional but recommendend] Create and activate virtual environment by:
+
+	- Going to the FORMS subdirectory
+	- Executing: **python** (or python3 or py depending on your system) **-m venv [virtual_environment_name]**
+	- Executing: **source [virtual_environment_name]/bin/activate**
+
+2. Install Flask + Flask-Cors (used to prevent annoying CORS error in local development) by executing:
+
+	- **pip install Flask-Cors**
+
+3. [Optional] Activate Debug Mode by setting the environment variable:
+
+	- FLASK_ENV=development (for instance by executing **export FLASK_ENV=development** in bash)
+
+4. Start the app by executing:
+
+	- **flask run**
+
+(vscode can do 3. and 4. automatically, and possibly the venv setup too)

+ 38 - 14
site/index.js

@@ -1,30 +1,54 @@
 
-// Executed when the page's DOM is loaded (at least in theory...)
+// Config data -- could be whatever
+// should be moved to a different file if/when there's a lot of it
+const flask_be_address = 'http://127.0.0.1:5000'
+
+
+// PAGE INITIALIZER -- function that gets auto-executed when the page's DOM is loaded
 $(function(){
 
+    // Handle 'simple_query' form submit
     $('#simple_query').submit(
+        
         function (e) {
             
+            // Prevent default handling of form
             e.preventDefault();
 
+            // Get form data, define custom object to be sent to the BE
             let f = $(this);
-
             let word = f.find('[id=query_text]').val();
             console.log(word)
-
-            let postData = {
+            //
+            // DTO: 'Data Transfer Object'
+            let queryDTO = {
                 word: word
             }
-            $.post('http://127.0.0.1:5000/simple_get_query', postData,
-                function(results){
-                    console.log(results)
-                    if(results.length==0){
-                        alert('No results!')
-                    }
-                    $("#result").html(results.join('<br/>'));
-                }    
-            );
+
+            // Do async post request to BE service
+            getData(queryDTO)
+                // After request finishes, process response data
+                .done(response => processData(response));
            
         }
     )
-});
+});
+
+
+// Separate function to handle HTTP request -- could be moved to different file (a service)
+function getData(queryDTO){
+
+    let url = flask_be_address.concat('/simple_get_query');
+
+    return $.post(url, queryDTO);
+}
+
+
+// Separate function to handle response data -- could be moved to different file (a service)
+function processData(response){
+    console.log(response)
+    if(response.length==0){
+        alert('No results!')
+    }
+    $("#result").html(response.join('<br/>'));
+}