0

I have a function calculate_full_eva_web(input:dict) it receives input dictionary several function applied on this input to create calculations dict, after calculations i want to send this data to html dashboard and after send data to html file i can play there with jinja stuff. i am unable to do so, i tried several ways but flask throws error. and also i don't know much about ajax ,may be ajax will do my work, let me know. that is why i am tagging ajax people on this post. Traceback is also attached..Thank you

In simple words, i want to send data to html in flask ! Please check my code. Let me know if i am doing anything wrong.

imports ...
from other file import other_functions
from other file import other_functions_2
from other file import other_functions_3

app = Flask(__name__, template_folder='templates/')

@app.route("/dashboard")
def calculate_full_eva_web(input:dict):

   calculate_gap = other_functions(input)
   calculate_matrix = other_functions_2(input)
   average = other_functions_3(input)
   data = dict{'calculate_gap':calculate_gap, 'calculate_matrix':calculate_matrix,'average':average}

   return render_template('pages/dashboard.html', data = data) 

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

Traceback

xpertdev
  • 1,293
  • 2
  • 6
  • 12
  • Can you explain how you pass a dictionary to /dashboard endpoint? – Dhruv Rajkotia Dec 01 '19 at 14:09
  • @DhruvRajkotia Thanks for your response, i saved the calculated values in to data variable , and then in return render_template(/pages/dashboard.html, data = data), – xpertdev Dec 01 '19 at 14:13
  • I think the input:dict part is wrong defined, you have to pass it like here. @app.route("/dashboard/") def calculate_full_eva_web(data): data = json.loads(data). you have to pass like this. – Dhruv Rajkotia Dec 01 '19 at 14:25
  • @DhruvRajkotia when i visit /dashboard its throws this error then The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. – xpertdev Dec 01 '19 at 14:35
  • Does this answer your question? [send data to html dashboard in python flask while having dictionary in a function parameter](https://stackoverflow.com/questions/59129695/send-data-to-html-dashboard-in-python-flask-while-having-dictionary-in-a-functio) – Dave W. Smith Dec 02 '19 at 05:29

1 Answers1

1

The route receive a dict as input so you must change @app.route("/dashboard") to @app.route("/dashboard/<input>") and pass input to the route in the link of the route.

For example, I have a route as below.

@app.route('/user/<name>') def user(name): return render_template('home.html', name=name) To pass name to the route, I access the link http://localhost:5000/user/myname.

do thuan
  • 459
  • 3
  • 11
  • input dict is a dictionary comming from other file in my project. yes when i remove my input:dict in function args, flask is able to show dashboard.html but without data, i have to pass input dict to this function so first it can do calculations on it and then i can render this data in this function,, or any other suggestion from your side ? – xpertdev Dec 01 '19 at 14:16
  • So you need to read the dictionary inside you route, do not pass it as a function parameter. – do thuan Dec 01 '19 at 14:21
  • if i tried your method several times before posting this question here also but then it throws this error like this "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again". – xpertdev Dec 01 '19 at 14:31
  • [This](https://flask.palletsprojects.com/en/1.1.x/quickstart/#variable-rules) may help you. – do thuan Dec 01 '19 at 14:39
  • its a right format , @app.route(/dashboard/) def index(input): return render_template('pages/dashboard.html') – xpertdev Dec 01 '19 at 14:39
  • what does `string:input` mean? – do thuan Dec 01 '19 at 15:11
  • to define datatype of input , @dhruv told me to add string or int befor your inputdict. anyways, its still not working . – xpertdev Dec 01 '19 at 15:12
  • can you tell me how can i send this data to html , i have to send this data in to table fields in html . – xpertdev Dec 01 '19 at 15:13
  • I don't think it will define input as a string. Try to replace `` with `` – do thuan Dec 01 '19 at 15:15
  • Also don't use `input` as a variable name. – do thuan Dec 01 '19 at 15:18
  • Thaun dont be sorry .. Thank you so much :) – xpertdev Dec 01 '19 at 16:30