-1

I have to make a DIY robot. In order to find out orientation of robot I am using my smartphone and PhonePi app. The app is based on Flask server. The code for getting orientation is given below and I named this one as orientation.py:

from flask import Flask 
from flask_sockets import Sockets

app = Flask(__name__)
sockets = Sockets(app)

@sockets.route('/orientation')
def echo_socket(ws):
    while True:
        message = ws.receive()
        raw2 = message.split(',')
        global orient1
        orient1 = float(raw2[0])
        orient2 = float(raw2[1])
        orient3 = float(raw2[2])
        print('x = ', orient1, "is", type(orient1))
        # print('y = ', orient2, "is", type(orient2))
        # print('z = ', orient3, "is", type(orient3))
        ws.send(message)


@app.route('/') 
def hello():
    return 'Hello World!'

if __name__ == "__main__":
    from gevent import pywsgi
    from geventwebsocket.handler import WebSocketHandler
    server = pywsgi.WSGIServer(('0.0.0.0', 5000), app, handler_class=WebSocketHandler)
    server.serve_forever()

I have to access variable orient1 from another script named robot controller.py. I tried to access variable several ways like:

from orientation import *
import orientation
from orientation import orient1

How can I access orient1 from other scripts within same folder?

davidism
  • 121,510
  • 29
  • 395
  • 339
Arun
  • 343
  • 3
  • 12
  • `orient1` is defined in the function `echo_socket` thus it can only be accessed from within this function. One option is to define it as a global variable (then the import will work). However, most of the time global variables are not used, and you can avoid them by changing the program architecture. – Mathieu Mar 22 '19 at 11:19

1 Answers1

0

The variable orient1 is inside a function, so you cannot access it from any place outside this function.

What you can do is to set a general variable outside the function so you can get it by a getter function, for example:

from flask import Flask 
from flask_sockets import Sockets

app = Flask(__name__)
sockets = Sockets(app)
orient1 = 0.0

def get_orient1():
    return orient1

@sockets.route('/orientation')
def echo_socket(ws):
    while True:
        message = ws.receive()
        raw2 = message.split(',')
        global orient1
        orient1 = float(raw2[0])
        orient2 = float(raw2[1])
        orient3 = float(raw2[2])
        print('x = ', orient1, "is", type(orient1))
        # print('y = ', orient2, "is", type(orient2))
        # print('z = ', orient3, "is", type(orient3))
        ws.send(message)


@app.route('/') 
def hello():
    return 'Hello World!'

if __name__ == "__main__":
    from gevent import pywsgi
    from geventwebsocket.handler import WebSocketHandler
    server = pywsgi.WSGIServer(('0.0.0.0', 5000), app, handler_class=WebSocketHandler)
    server.serve_forever()

Now, you can access it by:

from orientation import get_orient1
orient1 = get_orient1()
Eric
  • 1,108
  • 3
  • 11
  • 25