Created
August 18, 2017 12:45
-
-
Save vykhand/38a1eb29a82afbe8d4a408c7045b7e4c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This script runs the application using a development server. | |
It contains the definition of routes and views for the application. | |
""" | |
import adal | |
import json | |
from flask import Flask | |
import requests | |
app = Flask(__name__, static_url_path='/scripts') | |
# Make the WSGI interface available at the top level so wfastcgi can get it. | |
wsgi_app = app.wsgi_app | |
@app.route('/api/token') | |
def get_token(): | |
#https://login.windows.net/common/oauth2/authorize/ | |
# "clientSecret" : "verySecret="" | |
"""Renders a sample page.""" | |
context = adal.AuthenticationContext( | |
'https://login.microsoftonline.com/common', validate_authority=True, | |
api_version=None) | |
token_response = context.acquire_token_with_username_password( | |
'https://analysis.windows.net/powerbi/api', | |
'[email protected]', | |
'P00l!2017', '7a7fad9e-bb5f-4c63-bb2c-655ed25ffe85' | |
) | |
aad_token = token_response['accessToken'] | |
headers = {'Authorization': 'Bearer ' + aad_token} | |
response = requests.get('https://api.powerbi.com/v1.0/myorg/groups', headers=headers) | |
for id in json.loads(response.text)['value']: | |
group_id = id['id'] | |
break | |
response = requests.get('https://api.powerbi.com/v1.0/myorg/groups/' + group_id + '/reports', headers=headers) | |
for r in json.loads(response.text)['value']: | |
report_id = r['id'] | |
embedUrl = r['embedUrl'] | |
break | |
post_data = post_data = """ | |
{ | |
"accessLevel": "View" | |
} | |
""" | |
headers.update({'Content-type': 'application/json'}) | |
response = requests.post('https://api.powerbi.com/v1.0/myorg/groups/' + group_id + \ | |
'/reports/' + report_id + '/GenerateToken',data = post_data, headers=headers) | |
report_token = json.loads(response.text)['token'] | |
j = '{"embedToken":"' + report_token + '"}' | |
return j, 200, {'Content-Type': 'application/json'} | |
@app.route('/') | |
def start_page(): | |
html = '''\ | |
<html> | |
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script> | |
<script src="powerbi.js"></script> | |
<div id="reportContainer"></div> | |
<script> | |
// Read embed application token from Model | |
var accessToken = "@Model.EmbedToken.Token"; | |
// Read embed URL from Model | |
var embedUrl = "@Html.Raw(Model.EmbedUrl)"; | |
// Read report Id from Model | |
var embedReportId = "@Model.Id"; | |
// Get models. models contains enums that can be used. | |
var models = window['powerbi-client'].models; | |
// Embed configuration used to describe the what and how to embed. | |
// This object is used when calling powerbi.embed. | |
// This also includes settings and options such as filters. | |
// You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details. | |
var config = { | |
type: 'report', | |
tokenType: models.TokenType.Embed, | |
accessToken: accessToken, | |
embedUrl: embedUrl, | |
id: embedReportId, | |
permissions: models.Permissions.All, | |
settings: { | |
filterPaneEnabled: true, | |
navContentPaneEnabled: true | |
} | |
}; | |
// Get a reference to the embedded report HTML element | |
var reportContainer = $('#reportContainer')[0]; | |
// Embed the report and display it within the div container. | |
var report = powerbi.embed(reportContainer, config); | |
</script> | |
</html> | |
''' | |
return html | |
if __name__ == '__main__': | |
import os | |
HOST = os.environ.get('SERVER_HOST', 'localhost') | |
try: | |
PORT = int(os.environ.get('SERVER_PORT', '5555')) | |
except ValueError: | |
PORT = 5555 | |
app.run(HOST, PORT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment