27

I have a Flask script which creates a website and prints some data dynamically. - The data which it prints should come from another python script.

The current problem that I'm facing is that if I put the line that executes the python script before the line that executes the Flask app, it will run the Python script without running Flask; and vice versa.

Python script:

import websocket
from bitmex_websocket import Instrument
from bitmex_websocket.constants import InstrumentChannels
from bitmex_websocket.constants import Channels
import json

websocket.enableTrace(True)

sells = 0
buys = 0


channels = [
    InstrumentChannels.trade,
]


XBTUSD = Instrument(symbol='XBTUSD',
                    channels=channels)
XBTUSD.on('action', lambda msg: test(msg))


def test(msg):
    parsed = json.loads(json.dumps(msg))


    print(parsed)

XBTUSD.run_forever()

Flask script (NB: price should be the variable 'parsed' from the other script):

# Start with a basic flask app webpage.
from flask_socketio import SocketIO, emit
from flask import Flask, render_template, url_for, copy_current_request_context
from random import random
from time import sleep
from threading import Thread, Event
import requests, json
import time

__author__ = 'slynn'

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.config['DEBUG'] = True

#turn the flask app into a socketio app
socketio = SocketIO(app)

#random number Generator Thread
thread = Thread()
thread_stop_event = Event()

class RandomThread(Thread):
    def __init__(self):
        self.delay = 1
        super(RandomThread, self).__init__()

    def randomNumberGenerator(self):
        while not thread_stop_event.isSet():
            socketio.emit('newnumber', {'number': parsed}, namespace='/test')
            sleep(self.delay)

    def run(self):
        self.randomNumberGenerator()


@app.route('/')
def index():
    #only by sending this page first will the client be connected to the socketio instance
    return render_template('index.html')

@socketio.on('connect', namespace='/test')
def test_connect():
    # need visibility of the global thread object
    global thread
    print('Client connected')

    #Start the random number generator thread only if the thread has not been started before.
    if not thread.isAlive():
        print("Starting Thread")
        thread = RandomThread()
        thread.start()

@socketio.on('disconnect', namespace='/test')
def test_disconnect():
    print('Client disconnected')


if __name__ == '__main__':
    socketio.run(app)
2
  • 2
    Please introduce some of the code, you've tried so far. Also, you can use subprocess.call(["python","scriptname.py"], shell=true) to run a script in background while running another script. Oct 16, 2018 at 12:48
  • 1
    I added the code, in order to be more specific!
    – Jack022
    Oct 16, 2018 at 12:54

2 Answers 2

33

Using import:

  • Wrap what the python script (e.g. website_generator.py) is generating into a function.
  • Place it in the same directory as your app.py or flask.py.
  • Use from website_generator import function_name in flask.py
  • Run it using function_name()

You can use other functions such as subprocess.call et cetera; although they might not give you the response.

Example using import:

from flask import Flask
import your_module # this will be your file name; minus the `.py`

app = Flask(__name__)

@app.route('/')
def dynamic_page():
    return your_module.your_function_in_the_module()

if __name__ == '__main__':
    app.run(host='0.0.0.0', port='8000', debug=True)
9
  • I tried that too but for some reason it will still run one of the two parts before the other, i have no idea why, i'm trying again right now! I added my code
    – Jack022
    Oct 16, 2018 at 12:55
  • I dont see any import from your module; what are you trying to achieve? where is the content generated? and where is it going to be used? Oct 16, 2018 at 13:02
  • 3
    If you're actually calling test function; then you must return and not print. Oct 16, 2018 at 13:03
  • You don't see the import because i changed it after it wasn't working, to test other ways. The expected output is to have the data from XBTUSD.run_forever printed on the webpage in real time with asincronous updates
    – Jack022
    Oct 16, 2018 at 13:26
  • Sorry for not being specific! What i'm trying to achieve is to have the parsed data from test printed on my webpage. My main two problems are the one i specified in my question and that i can't call the variable "parsed" from a different function
    – Jack022
    Oct 16, 2018 at 13:27
4

try this:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def run_script():
    file = open(r'/path/to/your/file.py', 'r').read()
    return exec(file)

if __name__ == "__main__":
    app.run(debug=True)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.