clickable parameter in html go to python function using flask

175 Views Asked by At

i need help in python flask environment i have some html page that in that page iam getting list of ip addresses from SQL database and the ip addresses in the list are clickable. what i need is the ability to click on some IP and be able to use that ip in another function in FLASK.

part of code my code example: HTML

<!DOCTYPE html>
<html>
 {% extends "base.html" %}

{% block content %}
<body>
    <script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
    <script>
        function goPython(){
            $.ajax({
              url: "/clicked",
             context: document.body
            }).done(function() {
             alert('finished python script');;
            });
        }
    </script>



{% for Devices in ip %}

<form action = "http://192.168.1.1:8081/clicked" method = "post">
            <ul id="Devicesid">
        <li class="label even-row">
             <a onclick="goPython()" value="btnSend"><button type="button" name="btnSend">{{ Devices.ip }}</button></</a>

            </li>
</ul>

</form>
</body>
{% endfor %}


</html>
{% endblock %}

and part from the python main.py code:

    @main.route('/clicked',methods = ['POST', 'GET'])
def clicked():
    while True:
        IP = request.form['btnSend']
        URL = 'https://' + IP

        credentials = {'username': 'apiuser', 'secretkey': 'f00D$'}
        session = requests.session()
        ###and so on......

as you can see in the part of the HTML iam using FOR loop and getting the ip addresses from my database, now iam trying to have the ability for clicking on the IP address and use it in another function of python FLASK for connect to that practicular device.

how can i do it simple and correct ? as iam understand that for making it work i need to use AJAX or JQuery...

please help

1

There are 1 best solutions below

2
On

Try this below in your JS/HTML code :

    <!DOCTYPE html>
    <html>
     {% extends "base.html" %}

    {% block content %}
    <body>

        <script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
        <script>
            function goPython(currentIp){
                $.ajax({
                  type: "POST",
                  url: "http://192.168.1.1:8081/clicked",
                  data: {'current_ip' :currentIp},
                  success: success,
                  dataType: dataType
               });
            }
        </script>

    <form>
        <ul id="Devicesid">
        {% for Devices in ip %}
            <li class="label even-row">
                 <a value="btnSend"><button onclick="goPython(Devices.ip)" 
 type="button" name="btnSend">{{ Devices.ip }}</button></</a>
            </li>
        {% endfor %}

        </ul>
    </form>
    </body>
    {% endblock %}

    </html>

And in your Flask code,  do this :

    @main.route('/clicked',methods = ['POST', 'GET'])
    def clicked():
            IP = request.json.get('current_ip', '')
            URL = 'https://' + IP

            credentials = {'username': 'apiuser', 'secretkey': 'f00D$'}
            session = requests.session()
            ###and so on......