pythonpython-3.xflaskpython-webbrowserstreamlit

How to expose python input() function in UI/web , to get input from client(user) and pass it back to server


How to expose python input() function in UI/web , to get input from client(user) and pass it back to server. Then do some calculation and pass it back to client(user).

#Step 3 : I display his age back to UI

Randomstate=1
LR_classifier = LogisticRegression( penalty='l2',              # penalty : {'l1', 'l2', 'elasticnet', 'none'}
                                    dual=False,                # dual : {True, False}                                    
                                    tol=0.0001,                # tol : float
                                    C=1.0,                     # C : float
                                    fit_intercept=True,        # fit_intercept : {True, False} 
                                                               # intercept_scaling=1,
                                    class_weight=None,         #class_weight : {'balanced', }
                                    random_state=Randomstate,  # random_state: int 
                                    solver='lbfgs',            #solver : {'newton-cg', 'lbfgs', 'liblinear', 'sag', 'saga'}
                                    max_iter=100,              # max_iter : int
                                    multi_class='auto',        # multi_class: {'auto', 'ovr', 'multinomial'}
                                    verbose=0,                 # verbose : int
                                    warm_start=False,          # warm_start : {True, False} 
                                    n_jobs=None                # n_jobs : int
                                                               #l1_ratio=None,
                                )
LR_classifier.fit(X_train, y_train)
y_pred = LR_classifier.predict(X_test)
print('Accuracy of baseline logistic regression classifier on train set:  {:.2f}'.format(100*LR_classifier.score(X_train, y_train)))
print('Accuracy of baseline logistic regression classifier on test set :  {:.2f}'.format(100*LR_classifier.score(X_test, y_test)))

Solution

  • The answer for your question is quite a long one and I will try to summarise the process linking you to the documentation you need.

    First, create simple inputs in HTML such as a textarea and a submit button. Once the button is clicked you want to trigger a function in your client that posts a payload of information to the server. The server will process the data and send a response in the form of a JSON object back to the client, using jsonify from flask.

    The latter process is called AJAX and is quite used when doing a server/client communication.

    Intro to the steps of the process:

    1. Create simple inputs in HTML such as a textarea and a submit button to give users some input fields where they can type information in.

    HTML

        <textarea id="text" name="text" spellcheck="false", autocomplete="off" autofocus>##Type sth</textarea>
        <input type="button" id="clickme">
    
    1. In your client side trigger an event once the button is clicked and post your payload to the server.

    Client

    textEditor = document.getElementById("text");
    document.getElementById("clickme").onclick = func;
    
    
    function func(){
    
        console.log('start')
        
        $.post('/_get_payload', {
            text: textEditor.value
        }).done(function(data){
            // data is the payload received from the server
            console.log('success')
        }).fail(function(){
            console.log('fail')
        });
    
        console.log('end')
        
    }
    
    1. The server view function is called when the button is clicked and will send back to the client a JSON object if everything went smoothly.
    from flask import jsonify
    
    ....
    
    @app.route('/_get_payload', methods=['POST'])
    def get_payload():
        data = request.form['text']
    
        # prints the user input written in the textarea
        print(data)
    
        return jsonify({ 
            "sth": "sth"
        })
    

    Things you should bear in mind are: