Python SDK Quick Start using PyCharm
PyCharm by Intellij is a clean, intuitive Integrated Development Environment (IDE) for writing and testing programs written in Python. You can download the Community edition of PyCharm for free here. Once you have installed and started PyCharm on your local machine (from which you should be able to connect to Seeq via browser), the following steps will get you from zero to hyperspeed in Python in minutes flat. Let's get started!
Setting Up
From the File menu, choose "Create new project..."
In the resulting dialog, choose a location for your project and make a note of it - we will need to move some files into the resulting project folder in a later step.
In the same dialog box, click the gear icon on the Interpreter line and choose "Create VirtualEnv" - this optional step allows you to add packages to a specially-constructed version of the Python interpreter that may be reused by selecting the same VirtualEnv later for other projects.
Name the virtual environment something like seeq_virtualenv and choose python 2.7 as the base interpreter (not an option? time to install Python...note that the Seeq SDK is built on Python 2, not 3)
Choose to “inherit global site-packages” if you are already using Python for other projects and have packages installed on your base interpreter already.
Click PyCharm > Preferences… (or Settings…)
Under “Project”, select Project Interpreter and add a thing or two to your seeq_virtualenv:
six, certifi, and urllib3Grab an egg and put it in your project folder:
a) You can always get a copy of the Seeq Server SDK from www.seeq.com/customer-download which contains the Python egg file...it will be in a folder cleverly named "python" and should look something like seeq_sdk-0.40.05-py2.7.egg. If you happen to have access to the Seeq Server host, on Windows, the egg you need will probably be at C:\Program Files\Seeq Server\sdk\python\; on Ubuntu, look in the Seeq program folder for sdk/python.
b) Copy the egg file to your project folder using the file explorer, then change the extension from .egg to .zip (if your file explorer doesn’t show the extension by default, you can change this at least temporarily). Once the file is a .zip, extract the contents. You will find a folder named seeq_sdk, which should be placed in the Python project folder you noted in step 2 (this is actually just a regular folder that contains a folder called .idea that keeps track of the project settings).File > New… > Python File and name it whatever you like (we suggest hello_seeq)
Insert the following text into your hello_seeq (or whatever, you nonconformist, you!) file after it opens in the editor:
hello_seeq.py
# imports
import seeq_sdk as sdk
import getpass
# login prompt definition
def get_authenticated_client(api_url):
api_client = sdk.ApiClient(api_url)
auth_api = sdk.AuthApi(api_client)
auth_input = sdk.AuthInputV1()
auth_input.username = raw_input('Username:').rstrip().lower()
auth_input.password = getpass.getpass()
auth_input.auth_provider_class = "Auth"
auth_input.auth_provider_id = "Seeq"
auth_api.login(body=auth_input)
return api_client
"""
def get_authenticated_client(api_url):
api_client = sdk.ApiClient(api_url)
auth_api = sdk.AuthApi(api_client)
auth_providers_output = auth_api.get_auth_providers()
if len(auth_providers_output.auth_providers) == 1:
auth_provider_class = auth_providers_output.auth_providers[0].datasource_class
auth_provider_id = auth_providers_output.auth_providers[0].datasource_id
else:
print 'Select login Directory:'
for idx, auth_provider in enumerate(auth_providers_output.auth_providers):
print '(' + str(idx) + ') ' + auth_provider.name + '(' + auth_provider.datasource_class + ': ' + \
auth_provider.datasource_id + ')'
choice = raw_input('Choice: ')
valid_choice = False
while not valid_choice:
try:
auth_provider_index = int(choice)
valid_choice = auth_provider_index < len(auth_providers_output.auth_providers)
if not valid_choice:
choice = raw_input('Not a valid choice; try again: ')
except TypeError:
choice = raw_input('Not a valid choice; try again: ')
auth_provider_class = auth_providers_output.auth_providers[auth_provider_index].datasource_class
auth_provider_id = auth_providers_output.auth_providers[auth_provider_index].datasource_id
auth_input = sdk.AuthInputV1()
auth_input.username = raw_input('Username:').rstrip().lower()
auth_input.password = getpass.getpass()
auth_input.auth_provider_class = auth_provider_class
auth_input.auth_provider_id = auth_provider_id
auth_api.login(body=auth_input)
return api_client
"""
# main code of script, which makes use of the get_authenticated_client def above
seeq_api_url = 'http://localhost:34216/api'
seeq_api_client = get_authenticated_client(seeq_api_url)
seeq_system_api = sdk.SystemApi(seeq_api_client)
system_status = seeq_system_api.get_server_status()
print system_status
Running the Script
Not so fast, speedy! At this point, you will probably need to make at least one change to the above code to make it work for your system. First, unless you are running the script directly on the Seeq server using the default port of 34216, you'll want to change the line that starts with seeq_api_url to point to your Seeq server. Also, if you typically log into Seeq with the "Directory" field on the login screen set to something other than "Seeq", you'll need to determine the AuthProviderClass and AuthProviderId of the Directory used for authentication - see step 2.
Not using the "Seeq" Directory to log in? What a great opportunity to try out the API Reference! (You're going to want to get familiar with this, anyway.) From Seeq Workbench, click on the helper menu at the top-right of the browser window and choose "API Reference". Click on "Auth", then on the line that says "GET /auth/providers". Finally, click "Try it out!" and examine the response. You should see a curly-brace {} delimited block with a line like "name": "YourDirectoryNameHere". Make a note of the datasourceClass and datasourceId for this block and replace "Auth" and "Seeq" in the code snippet above with their respective values. Most likely, these will be "LDAP" and a GUID with double-quotes around it. Alternatively, you can comment out the Auth/Seeq-only version of get_authenticated_client and uncomment the more robust version shown in the """-enclosed comment block above.
Okay, all set. At this point, you have three options to run the script, right there from PyCharm. The first two, "Debug" and "Run", can be accessed by right-clicking in the text editor portion of the PyCharm environment.
"Debug" - you'll use this option if you want to set break points and step through your code. Don't worry if you don't know what this means - you'll get there! For now, just be aware that if you choose this mode, the password you type in when the call to getpass.getpass() (it gets your password) will be echoed, i.e., shown on the screen. If no one is peering over your shoulder right now, you're probably good, though.
"Run" - this option may also echo your password, but it doesn't instrument your code for debugging (meaning in some cases it might be faster) - as such, any breakpoints you set will not be honored.
"Terminal" - in this case, you will be opening a Mac Terminal or a Windows Command Prompt. If you don't see a button that says "Terminal" at the bottom of the PyCharm window, you can add the Terminal view by clicking View > Tool Windows > Terminal in the File bar of the window. From here, you can run the script by typing "python hello_seeq.py" (sans quotes). With this approach, your password will be safely displayed as:
Celebrating your Success
Well, let's just assume you got a response like:
{'admin_contact_email': '[email protected]',
'admin_contact_name': 'Pete Seeqer',
'installation_id': 'ABCD123HIJKLMN0P',
'status': 'OK',
'telemetry_enabled': True}
If not, you can always contact us for more assistance. Happy Seeqing!