Fill option with Flask data - Front x Backend

Asked

Viewed 106 times

0

I have little knowledge in frontend and I’m needing to do the following:

In my Backend I have a function which I run in python and which returns a list according to print.

app = Flask(__name__)

@app.route('/')
def info():
    return 'Navegar para os dados'

@app.route('/Teste')
def mensagem():
    fd='var_fd'
    dt='var_dt'

    _engine = sqlalchemy.create_engine("minha_string_de_conexao")
    sql = """
    select data 
        from minha_tabela
    where codigo like '%{0}%'
    and data={1!r}
    order by 1 desc; 
    """.format(fd, dt)

    pos = pd.read_sql_query(sql,_engine)

    value = str(pos.iloc[0:]['data'].tolist())
    return str(value)

if __name__ == '__main__':
    app.run()

tela do navegador

<div class="col-md-12" align="right" style="font-size:12px;color:black;margin-left:10ox;margin-bottom:15px;">
    <select class="form-control" style="width:120px">
    <option selected value="">hh:mm:ss</option>
</div>

I need the feedback of that data to be populated in the combobox. I believe that with jquery is a good option, however, I am not able to realize this demand.

As I mentioned initially, I don’t have much knowledge in front. Grateful!

Could someone help me?

1 answer

1

You can use a simple AJAX with jQuery. Put a id in the select to have a reference:

<select id="horas" class="form-control" style="width:120px">
   <option selected value="">hh:mm:ss</option>
</select>

AJAX:

<script>

    $(document).ready(function(){

       $.get("http://127.0.0.1/Teste", function(data){

          var opts = '';

          $.each(data, function(i, v){

             opts += '<option value="'+ v +'">'+ v +'</option>';

          });

          $("#horas").append(opts);

       }, "json");

    });

</script>

As the return will come an array in the form of a string, the option "json" will convert the string to array.

  • Thank you very much for your return. I ended up with a question. At what point does AJAX call the function that is in Python? I must adapt the code you sent me?

  • 1

    Here: $.get("http://127.0.0.1/Teste", function(data){, the first information between quotes is the URL from which the return of the back end will come, ie. http://127.0.0.1/Teste.

  • 1

    I don’t understand Phyton, but in the file where is generated the array that you showed in print, you should return this array to AJAX... In PHP it would be a echo.

  • 1

    Since AJAX is an HTTP request, it is as if you made a file in Phyton that prints on the screen only this array, and AJAX will pull it in string form.

  • Perfect Sam. From what I understand, the step you explained to me "picks up" the URL data that represents the return of the back end. However, for this URL to be generated, I need to call the function in Python which in this case is the def message().

  • 1

    That. Basically you should generate a return for AJAX, something that generates a string. I think in Phyton it would be a kind of print().

  • 1

    From what I see, the function of your Phyton has a Return... you should print this return so that AJAX receives it.

  • 1

    Try to mark in the answer of your questions that solved the question. I was looking at your question history and saw that none was finished with . Users' effort to answer questions is rewarded when you score the best answer or vote positive on the ones you found useful. Abs!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.