How to get values from my python file to my form using Flask?
<p>I'm trying to extract "address" from my python file "get interfaces.py" display it into my form using flask, but my code is not working, and may be I'm doing it wrong. I'm new in Flask </p>

<p>Here's my python file get_interfaces.py</p>

<pre><code>with open('/etc/network/interfaces', 'r+') as f:
    for line in f:
        found_address = line.find('address')
        if found_address != -1:
            address = line[found_address+len('address:'):]
            print 'Address: ', address
        found_network = line.find('network')
        if found_network != -1:
            network = line[found_network+len('network:'):]
            print 'Network: ', network
        found_netmask = line.find('netmask')
        if found_netmask != -1:
            netmask = line[found_netmask+len('netmask:'):]
            print 'Netmask: ', netmask 
        found_broadcast = line.find('broadcast')
        if found_broadcast != -1:
            broadcast = line[found_broadcast+len('broadcast:'):]
            print 'Broadcast: ', broadcast
</code></pre>

<p>Where the variable "address" is what I want.</p>

<p>My flask: </p>

<pre><code>from flask import render_template, request, redirect, url_for, Response
from app import app, lm
from flask.ext.login import login_required, login_user, logout_user, UserMixin
from Mensajeria.ConfiguracionInterfaces import ConfiguracionInterfaces

import zmq
import struct
import subprocess
import os
import threading, time, json
import cProfile, pstats, StringIO

@app.route('//')
def ibisaserver_cliente():
    pr = cProfile.Profile()
    pr.enable()
    Ci = ConfiguracionInterfaces()
    pr.disable()
    s = StringIO.StringIO()
    sortby = 'calls'
    ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
    ps.print_stats()
    logging.debug(s.getvalue())
    return render_template('test.html',
                           title='test',
                           Ci=Ci)
</code></pre>

<p>Here's my configuracioninterfaces.py :</p>

<pre><code>import ConfigParser
import os


class ConfiguracionInterfaces(object):

    def __init__(self):
        self.config = ConfigParser.RawConfigParser()

        #Ruta para archivo en PC:
        self.rutaExterna = os.path.join(os.getcwd(), "app/Mensajeria/get_interfaces.py")
        self.config.read(['get_interfaces.py', self.rutaExterna])
        #Ruta para archivo en MOXA:
        #self.config.read('/home/moxa/properties.cfg')

    def obtenerAddress(self):
        endPoint = self.config.get('address', 'address')
        return address

    def modificarAddress(self, address):
        self.config.set('address', 'address', address)
        #with open('/home/moxa/properties.cfg', 'wb')as archivo:
        with open(self.rutaExterna, 'wb') as archivo:
            self.config.write(archivo)
</code></pre>

<p>My HTML:</p>

<pre><code>&lt;form method="post" name="prueba"&gt;
  &lt;div class="form-group"&gt;
    &lt;label class="col-sm-3 col-sm-3 control-label"&gt;prueba:&lt;/label&gt;
    &lt;div class="col-sm-9"&gt;    
      &lt;input type="text" class="form-control" name="address" value="{{ Ci.obtenerAddress() }}" disabled&gt;
    &lt;/div&gt;
  &lt;/div&gt; 
&lt;/form&gt;
</code></pre>

<p>In <code>get_interfaces.py</code> create function (ie. <code>get_info()</code>)<br>
with your code and with <code>return address</code> at the end.</p>

<p>In flask file  run</p>

<pre><code>import get_interfaces

address = get_interfaces.get_info()
</code></pre>

<hr>

<p>BTW: if your code can return more than one address then create list with this addresses and use <code>return list_with_addresses</code></p>

<p>And in flask file you will have</p>

<pre><code>all_addresses = get_interfaces.get_info()

first_address = all_addresses[0]
second_address = all_addresses[1]
</code></pre>

python html django python-2.7 flask