Cancel
Showing results for 
Search instead for 
Did you mean: 

Python starter app

Genius
Genius

Hi all,

 

Wanted to share my basic python/flask/jinja2 app for those who are getting started with MindSphere/Web development like I am.

 

https://github.com/rexkc/mdsp-python-demo

 

At the moment it is very basic, using just the asset/aspect and time series APIs. I will slowly update it with more things in the future such as MongoDB integration, more APIs and error handling etc. If you found it hard to follow please let me know so I can update the documentation better.

 

Cheers,

 

Rex

10 REPLIES 10

Re: Python starter app

Siemens Creator Siemens Creator
Siemens Creator

Thanks for sharing this.

 

I have one question:

Since this app is hosted on CF within MindSphere and you start it via MindSphere Launchpad I wonder why it is necessary to enter hard-coded credentials.Could you maybe explain that?

 

Reason behind this question:

As far as my understanding goes, if you sign-in to mindsphere you will get a session with an authentication token in its header, that you can should be able to use for your requests.

 

You should be able to extract the token via:

current_auth_header = request.headers.get('Authorization', None)
headers = {'Authorization':current_auth_header }
r = requests.get("myApiCall", headers=headers)

 

 

Re: Python starter app

Genius
Genius

Edit: Done, thanks for the feedback! If you clone the repo now it should ignore using the client credentials if just pushing to MindSphere.

 

Hi @_And_,

 

Thanks for the feedback! perhaps my README wasn't clear, I will re-word it.

 

The app actually checks if it is running in CF and then determines if it will use its own Authentication or the client credentials. The client credentials are only needed for debugging when not running in MindSphere.

 

Having said that, I shall also update it so that if pushing to MindSphere, you will not need the vars.py file.

Re: Python starter app

Siemens Creator Siemens Creator
Siemens Creator

Thanks for adapting and also adding vars.py directly to the repo, but I also could have interpreted the following line in code in the first place - shame on me :-)

 

try:
  int(os.getenv("VCAP_APP_PORT"))!= 5000 #app is running in cloud foundry
  headers = {'Authorization': request.headers["Authorization"]}

By the way: What about this port = 5000. Does ist have a special meaning in CF?

Re: Python starter app

Genius
Genius

 

The check is actually just

os.getenv("VCAP_APP_PORT")

As the local debugging app won't have "VCAP_APP_PORT" and will raise the exception.

 

 

The 5000 here has no meaning for Cloud Foundry. It is is the default python debugger port.

Re: Python starter app

Legend
Legend

I would recommend using the `VCAP_APPLICATION` env variable to auto-detect the CloudFoundry environment, and then go from there to detect the rest of the configuration (services, ports, etc).

 

You can take some hints from the Node.JS cfenv project:

https://github.com/cloudfoundry-community/node-cfenv

Re: Python starter app

Siemens Creator Siemens Creator
Siemens Creator

 


@dlouzan wrote:

I would recommend using the `VCAP_APPLICATION` env variable


Thats a good approach, since apparently VCAP_PORT is also deprecated according to https://docs.cloudfoundry.org/devguide/deploy-apps/environment-variable.html - so maybe it will be gone compltely in a future release of CF

 

And another thing that I thought about:

If a MindSphere app was hosted somewhere else externally (using the "self-hosted" mode in the Developer Cockpit) then the CF-check would of course fail - but still there would be an authentication header that could be grabbed and reused.

To have something like a general wrapper it could be done like this:

 

try if app is running in a CF environment and extract headers using approach above
   if this fails
      try getting existing authentication headers anyway and test them with a dummy call to MindSphere-API
         if this also fails
             get authentication header via hardcoded user-credentials

 

 

Re: Python starter app

Experimenter
Experimenter

Hello,

many thanks to share this demo application!  It really help to get my python app running on MindSphere!

 

One question I have is about the production server. I see that you still start your app with the flask development server. But which production server would you recommend to use in the MindSphere?

 

Best Regards

Eddie

Re: Python starter app

Experimenter
Experimenter

Hello,

 

the repository moved to https://code.siemens.com/ms-aunz/mdsp-python-demo it seems that it is not possible for me to access the code as an non Siemens employee.

 

Is there a way to get access to the repository?

 

Best regards

Martin

Highlighted

Re: Python starter app

Genius
Genius

Hi Martin,

 

Here you go.