Reputation: 7273
Here is the Flask code that I am trying to run:
import threading
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def index():
df = pd.read_csv('data').drop('Open', axis=1)
chart_data = df.to_dict(orient='records')
chart_data = json.dumps(chart_data, indent=2)
data = {'chart_data': chart_data}
return render_template("index.html", data=data)
app.run(host="0.0.0.0",port=2000,debug=True)
The above code ran without error, so I included my tensorflow after that. But nothing works after the app.run()
.
import threading
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def index():
df = pd.read_csv('data').drop('Open', axis=1)
chart_data = df.to_dict(orient='records')
chart_data = json.dumps(chart_data, indent=2)
data = {'chart_data': chart_data}
return render_template("index.html", data=data)
app.run(host="0.0.0.0",port=2000,debug=True)
import tensorflow as tf
print("Tensorflow version: ",tf.__version__)
I tried running the app in separate thread but it is not working at all.
threading.Thread(app.run(host="0.0.0.0",port=2000,debug=True)).start()
Please advise me how I can run the code that is after app.run().
EDITED:
After the suggest from
I tried this:
import threading
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/result")
def index():
df = pd.read_csv('data').drop('Open', axis=1)
chart_data = df.to_dict(orient='records')
chart_data = json.dumps(chart_data, indent=2)
data = {'chart_data': chart_data}
return render_template("index.html", data=data)
app.run(host="0.0.0.0",port=2000,debug=True)
@app.route("/")
import tensorflow as tf
And I got the following error:
import tensorflow as tf
^
SyntaxError: invalid syntax
Upvotes: 1
Views: 905
Reputation: 6606
Flask's app.run(..)
is blocking.
If you want to use Tensorflow import it prior to starting the server and call logic within a route.
I am trying to run the training and want to display the output after that using the flask template.
Sounds like you're trying to do something similar to Tensorboard. Do you know about this? https://www.tensorflow.org/tensorboard/get_started
Otherwise, perform the training prior to app.run(...)
. The downside is that server will not start until training is done (which may be fine).
This approach is super easy to setup (no messing with Threading or Process), just run each file in a different console.
(Please take note this is pretty much what Tensorboard does, it just does it in a much more advanced fashion)
Upvotes: 2
Reputation: 192023
Simply print before starting the server.
You'll have to import tensorflow before you use it anyway, so move imports to the top, too
import json
from flask import Flask, render_template
import tensorflow as tf
import pandas as pd
app = Flask(__name__)
...
print("Tensorflow version: ",tf.__version__)
app.run(host="0.0.0.0",port=2000,debug=True)
However, makes more sense as a route
@app.route('/tf-version')
def tf_version():
return tf.__version__
Upvotes: 1