Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unable to call function in Flask
#1
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)
Reply
#2
Where were you expecting it to ask you something? In the console? In your browser?
Reply
#3
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.
Reply
#4
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
Reply
#5
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
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask run function in background and auto refresh page raossabe 2 7,526 Aug-20-2022, 10:00 PM
Last Post: snippsat
  IndexError: list index out of range" & "TypeError: The view function f: Flask Web App joelbeater992 5 3,507 Aug-31-2021, 08:08 PM
Last Post: joelbeater992
  gettin flask to call script periodically GrahamL 1 2,219 Jan-08-2021, 06:11 PM
Last Post: ndc85430
  how to pass javascript variables to url_for function in a flask template experimental 5 6,397 Oct-29-2020, 03:29 AM
Last Post: universe
  Flask Create global response function to be called from every where in the web app umen 2 2,299 Apr-14-2020, 09:54 PM
Last Post: umen
  flask-SQLAlchemy query with keyword as function argument pascale 2 3,477 Mar-13-2019, 08:45 PM
Last Post: Ecniv

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020