Reputation: 13
I am very new to coding and web development. I am a Systems Engineer and looking to get into the Web Development side of things. I took some python tutorials and pieced together a (probably very) rough python application. I would now like to take that application and put it on a website I have created so that I can allow others in the office to use the utility as well.
To that end, I installed transcrypt with the goal of converting the python code to javascript. When running transcrypt I get the following output:
Error while compiling (offending file last): File 'c:/Scripting/Transcrypt/Meraki.py', line 1, at import of: File 'c:/users/dab404/appdata/local/programs/python/python36/lib/site-packages/requests/init.py', line 43, at import of: File 'c:/users/dab404/appdata/local/programs/python/python36/lib/site-packages/urllib3/init.py', line 8, namely: Attempt to import module: connectionpool Can't find any of: c:/Scripting/Transcrypt/connectionpool.py c:/Scripting/Transcrypt/javascript/connectionpool.mod.js
The error goes on to list about 10 other files that it needs to run. I am not sure how to fix this issue and would appreciate any help anyone can give me.
Here is my code:
import requests
import json
from meraki import meraki
base_url = "https://dashboard.meraki.com/api/v0/"
def List_Orgs(apikey): #A FUNCTION FOR LISTING ORGANIZATION ADMINS
myOrgs = meraki.myorgaccess(apikey)
for orgs in myOrgs:
print(orgs)
def List_Admins(URL_admin, headers):
x = requests.get(URL_admin, headers = headers)
myAdmins = x.json()
for admins in myAdmins:
print(admins)
def Add_Admin(URL, admin_data, headers): #FUNCTION FOR ADDING NEW ADMIN
TO AN ORGANIZATION
r = requests.request("POST", URL, data = admin_data, headers = headers)
print(r.status_code)
if (r.status_code) == 201:
print()
print()
print("Administrator successfully added!")
print()
else:
print()
print("Administrator was NOT successfully added. Please try again!")
print()
def Del_Admin(URL_del, headers): #FUNCTION FOR DELETING AN ADMIN FROM AN
ORGANIZATION
r = requests.request("DELETE", URL_del, headers = headers)
print(r.status_code)
if (r.status_code) == 204:
print()
print()
print("Administrator successfully deleted!")
print()
else:
print()
print("Administrator was NOT successfully deleted. Please try again!")
print()
apikey = input("What is your Meraki API key? ")
print()
print("******************************************")
print()
print("Here is a list of your Organizations. You will need the ID to answer
the next set of questions.")
print()
print()
List_Orgs(apikey)
print()
print()
headers = {
'X-Cisco-Meraki-API-Key': apikey,
'Content-Type': "application/json"
}
add_or_del = input("Would you like to add or delete an admin? ")
if add_or_del == ("add" or "Add" or "ADD"):
orgid = input("Which Organization would you like to add an admin to? ")
admin_name = input("What is the new Admin's First and Last name? ")
admin_email = input("What is " + admin_name + "'s email address? ")
admin_access = input("What level of access would you like " + admin_name +
" to have? (full or read-only) ")
admin_data = '{\n\t\"name\":\"' + admin_name + '\",\n\t\"email\":\"' +
admin_email + '\",\n\t\"orgAccess\":\"' + admin_access + '\"}'
URL = (base_url + 'organizations/' + orgid + '/admins')
Add_Admin(URL, admin_data, headers)
elif add_or_del == ("delete" or "Delete" or "DELETE"):
orgid = input("Which Organization would you like to delete an admin from?
")
URL_admin = (base_url + 'organizations/' + orgid + '/admins/')
print()
print("Here is a list of Admins in this Organization. You will need to
admin ID to answer the next question.")
print()
print()
List_Admins(URL_admin, headers)
print()
print()
adminid = input ("What is the admin's Meraki portal ID? ")
URL_del = (base_url + 'organizations/' + orgid + '/admins/' + adminid)
Del_Admin(URL_del, headers)
else:
print("Please type add or delete and try again.")'
Thanks! David
Upvotes: 1
Views: 194
Reputation: 7000
The problem lies with the imports:
import requests
import json
from meraki import meraki
A module like requests
is a standard Python module that isn't supported by Transcrypt, since it uses code written in C, that doesn't run in a browser.
For json
there's a JavaScript counterpart that can be used directly from Transcrypt without problems.
Module meraki
I don't know, so can't judge about.
Although a growing number of standard modules is supplied in the Transcrypt distribution, in general it makes use of JavaScript modules, since these are specially geared toward functionality that makes sense in a browser.
E.g. local file access is generally prohibited in a browser, so any module using that cannot do it's 'thing'.
See also:
http://www.transcrypt.org/docs/html/what_why.html#the-ecosystem-different-batteries
So in Transcrypt you program in Python, but the libs you use are mainly JavaScript. The exception are very common libs like math, cmath, random (partially), time, datetime, itertools, re etc.
To get an impression of how to use JavaScript libs from Transcrypt, take a look at:
http://www.transcrypt.org/examples
and also at:
http://www.transcrypt.org/docs/html/integration_javascript.html#mixed-examples
[EDIT]
I've taken another good look at your application, and I notice that it's a typical console application, using things like input
and print
. While these are supported in Transcrypt in a limited way, see
in general web applications work somewhat differently.
In general they are event driven, meaning that a number of GUI elements are pieced together, sometimes in HTML, sometimes in a script. These GUI elements then trigger events, that in turn trigger certain pieces of code (event handlers) to be run.
So a good next step may be to study this way of working. A nice, simple example in Transcrypt, of HTML/DOM and a script cooperating in this way is this one:
http://www.transcrypt.org/docs/html/installation_use.html#your-first-transcrypt-program
In many cases with a web application there's also interaction with a webserver, so part of the processing is done on the server.
You can e.g. use Bottle or Django for that, as is demoed at:
https://github.com/Michael-F-Ellis/NearlyPurePythonWebAppDemo
Upvotes: 1