Python Forum
unable to call function in Flask - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: unable to call function in Flask (/thread-190.html)



unable to call function in Flask - verb - Sep-28-2016

Hello everyone
i am trying to call function from another function but unfortunately it doesn't work
here is my code... The app starts and it doesn't ask for dir name as i was expecting

from flask import Flask,render_template
from os import listdir
from os.path import join,isfile

app = Flask(__name__)

def ask_for_dir():
    dirpath=raw_input("please provide dirname for gallery")
    spisak={}
    for i in listdir(dirpath):
        pat=join(dirpath,i)
        if isfile(pat) and pat.endswith("jpg") or pat.endswith("jpeg"):
            spisak[i]=pat
        else:
            continue
    return spisak

@app.route('/')
def hello_world():
    spisak=ask_for_dir()
    lensp=len(spisak)
    return render_template("gal2.html",lensp=lensp,spisak=spisak)


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



RE: unable to call function in Flask - nilamo - Sep-28-2016

Where were you expecting it to ask you something? In the console? In your browser?


RE: unable to call function in Flask - snippsat - Sep-28-2016

As mention by nilamo,
this should probably by used in browser and not from console.

As a example.
index.html
<form action="dir_name" method="POST">
  Please provide dirname for gallery:
  <input type="text" name="gallery" value="">
  <input type="submit" value="Submit">
</form>
 
app.py
from flask import Flask, render_template, request

app = Flask(__name__)

@app.route('/')
def template():
   return render_template('index.html')

@app.route('/dir_name', methods=['POST'])
def ask_for_dir():
    dirpath = request.form['gallery']
    return(dirpath)

if __name__ == '__main__':
   app.run(debug=True)
You have to think of this in a different way it's when working with web.
Here is raw_input() replaced with html input.
Getting value from html form then in ask_for_dir() use request.form[]
Then you can work further with dirpath value in the function.


RE: unable to call function in Flask - verb - Sep-29-2016

so there is no way to ask the user in console and pass this value to flask ?? I want to do it this way because when i start the app i will pass the dir where the app will browse,also it's more friendly if i ask for dir not passing arguments to the app


RE: unable to call function in Flask - verb - Sep-29-2016

i have found the solution of my problem here is the code
from flask import Flask,render_template
from os import listdir
from os.path import join,isfile

app = Flask(__name__)

def ask_for_dir():

    spisak={}
    for i in listdir(app.dirpath):
        pat=join(app.dirpath,i)
        if isfile(pat) and pat.endswith("jpg") or pat.endswith("jpeg"):
            spisak[i]=pat
        else:
            continue
    return spisak

@app.route('/')
def hello_world():
    spisak=ask_for_dir()
    lensp=len(spisak)
    return render_template("gal2.html",lensp=lensp,spisak=spisak)


if __name__ == '__main__':
    app.dirpath=raw_input("please provide dirname for gallery")
    app.run(debug=True) 
so i moved the input function just before starting the app loop ::) anyway thanks to everyone who tried to help me


RE: unable to call function in Flask - nilamo - Sep-29-2016

Why are you building a webapp if you want to interact via the console? I think you should probably make it a config value, or make it a command line argument, that way if you restart the server you don't have to type anything at all.