Reputation: 133
I have a very Python-ic script which compiles with Transcrypt, but the issue is that the one outside dependency I have is that I need to import google-cloud-bigquery
. This obviously throws an error during the transpiling process, but it seems that the API is available in JavaScript (which is my target compilation) via <script src="https://apis.google.com/js/client.js">
But Transcrypt transpiles my index.py file, and I can't just place this JS script within the Python file (that I know of), so how do I implement it?
I know that other modules, such as Numscrypt, are available through Transcrypt but how do you actually add the module within the Python file?
Upvotes: 0
Views: 1469
Reputation: 476
You will need to use the JavaScript version of the library, and putting the import in a <script>
tag of the HTML file as previously described is the easiest way. Since the library will be in the global namespace at that point, you can make calls to it from anywhere in your Python program.
If you are using a bundler like Parcel or Webpack and use npm to store the libraries locally, you can use the Node.js require()
function and assign it to a Python variable like:
fabric = require('fabric')
Otherwise, if you need to load the JS library from a hosted location and want to do it from within Python, you can do it with JavaScript using a Transcrypt pragma compiler directive like this as mentioned in the Transcrypt docs:
fabric = __pragma__ ('js',
'''
(function () {{
var exports = {{}};
{} // Puts fabric in exports and in global window
delete window.fabric;
return exports;
}}) () .fabric;
''',
__include__ ('com/fabricjs/fabric_downloaded.js')
)
Upvotes: 1
Reputation: 10464
Load the JavaScript library in the HTML page your Transcrypt code hangs off from. Then you should be able to access the top level object of Google's JavaScript Client Library in your Transcrypt module.
I am not familiar with Google's JavaScript Client Library, but from glancing at the doco I guess that gapi
is the main object client.js exposes.
Schematically you could do something like this: First the HTML file index.html:
<html>
<head>
<meta charset="utf-8">
... other google scripts you might need ...
<script src="https://apis.google.com/js/client.js"></script>
</head>
<body>
... here your page elements...
<script type="module">import * as main from './__target__/your_google_stuff.js';</script>
</body>
</html>
Then in the same directory have the Transcrypt module your_google_stuff.py:
def do_your_stuff():
def auth_callback(res):
if res and res.error is None:
# Authenticated!
...do your stuff after authentication here
# here do the authentication, etc
gapi.client.setApiKey(YOUR_API_KEY) # Note that gapi should be globally available here!
gapi.auth.authorize({
'client_id': YOUR_ID,
... more atributes here...
}, auth_callback)
gapi.load(..., do_yor_stuff)
Compile your_google_stuff.py with Transcrypt and view your results by serving index.html.
Upvotes: 0