from flask import Flask, render_template
from flask_bootstrap import Bootstrap
from bokeh.embed import server_document, server_session
from bokeh.client import pull_session
import os
import socket

app = Flask(__name__)
bootstrap = Bootstrap(app)

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

@app.route('/MyApp')
def data_volume():
    bokeh_app = 'bokeh/DataVolume/'

    app_ip = socket.gethostbyname(os.environ['BOKEH_SERVER_HOSTNAME'])
    app_port = os.environ['BOKEH_SERVER_PORT']
    app_url = 'http://%s:%s/%s' % (app_ip, app_port, bokeh_app)

    my_session = pull_session(url=app_url)
    host_url = '%s/%s' % (os.environ['HOST_IP'], bokeh_app)

    script = server_session(session_id = my_session.id,
                               url = host_url,
                            relative_urls=True)

    return render_template('bokeh_app.html', title='Data Volume', script=script)
