Close python script and reopen it - python

i have a python script that i want to close console and then execute again the script from the beginning. Can you help me with this?
I need to close it when i execute a function on the script. Then reopen the file so it's ready again for doing something.

You should import your script instead. You can import python files from same directory. An example:
rerun.py:
def print_stuff():
print('stuff')
runner.py:
from rerun import print_stuff
print_stuff()

Use flask. It is made for this. Go take a look at http://flask.pocoo.org/docs/1.0/quickstart/.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def some_function():
print('do stuff here')

Related

How do I detach from the shell when running pywebio app in server mode?

I am running a simple web app using pywebio. If I use the start_server, it doesn't detach from the shell (is it expected to?)
I can background python itself, but that doesn't seem the right thing to do. If I dockerize this app, the docker run does not yield the shell.
What is the right way to handle this?
from pywebio.output import *
from pywebio.input import *
from pywebio import *
def myfunc():
...
if __name__ == "__main__":
start_server(myfunc, port=1234)

Launch flask python file from main python file

I have a flask python file that i want to open when i start the main python file.
main python file
start flask python file
continue with it's own independent processes (threading)
Which solution to take since i do not want the execution of the flask app to hinder the performance of the later processes. Not sure if i should do a subprocess or exec file?
both files are pretty independent of each other.
if I understood correctly, you can create a daemon thread for flask and continue with the execution of main program.
If you have a independent_module.py like this:
# independent_module.py
# your independent functions
def start():
pass
Then your main file would look something like this:
# main.py file
import threading
from flask import Flask
import main
app = Flask(__name__)
#app.route("/health")
def health():
return "OK"
#app.route("/ping")
def ping():
return "Et. Voila!!"
def run_server_api():
app.run(host='0.0.0.0', port=8080)
def main():
flask_thread = threading.Thread(target=run_server_api, daemon=True)
flask_thread.start()
# continue with your main program functions
independent_module.start()
if __name__ == "__main__":
# execute main
main()
You can simply execute python main.py

Calling Functions in Flask

Apologies in advance as this is probably the most basic question to be found here but I'm the greenest of newbies and cannot get my head around how to call a function in flask so it runs when I land on the URL.
My purpose is to try and get a python script to run when a GET request is made to the URL from WebCore (for those who don't know it's a program that allows you to code smart home functionality for SmartThings) or when I simply land at the URL. I will then tie this to a virtual switch which will start the code which controls a motor in a cat feeder so I can feed my cat remotely/by voice.
All very frivolous stuff but trying to learn some basics here, can anyone help?
As it stands I have two files, both in a root directory named 'CatFeeder'
catfeeder.py
from flask import Flask
from feed import start
app = Flask(__name__)
#app.route('/')
def feed()
return feed.start
if __name__ == '__main__':
app.run(host='0.0.0.0', port='5000', debug=True)
feed.py
import time
import RPi.GPIO as GPIO
def start():
# Next we setup the pins for use!
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
Motor1Forward = 17
Motor1Backwards = 18
GPIO.setup(Motor1Forward,GPIO.OUT)
GPIO.setup(Motor1Backwards,GPIO.OUT)
print('Feeding Lola')
# Makes the motor spin forward for x seconds
# James Well Beloved - 11 seconds = 28g food (RDA Portion = 52g/4kg cat or 61g/5kg cat)
# XXXX - X seconds - Xg food
GPIO.output(Motor1Forward, True)
GPIO.output(Motor1Backwards, False)
time.sleep(11)
print('Lola Fed!')
GPIO.output(Motor1Forward, False)
GPIO.output(Motor1Backwards, False)
GPIO.cleanup()
quit()
When I set export FLASK_APP=catfeeder.py and then flask run the service runs but nothing happens when I land on the page. I assume there is something wrong in the way I am calling things.
I guess it would be easiest if I just integrated the code from feed.py into catfeeder.py but I wasn't sure what the syntax would be for this and it felt like a messy way to go about it.
Thanks in advance!
you've imported the function but didn't actually invoke it as you missed adding your brackets (), try return start()
In case you meant to return a function object and not invoke the function, you should return it by typing return start

Python program seems to be running twice [duplicate]

This question already has answers here:
How to stop Flask from initialising twice in Debug Mode? [duplicate]
(2 answers)
Closed 8 years ago.
I have a python program which is running Flask. I noticed a strange thing, it looks like the program is running twice, which I do not want.
Here is the file for starting the program(runserver.py, in the root folder /):
from myapp import app
if __name__ == "__main__":
print "woho"
app.run(host='0.0.0.0',debug=True)
When running this, I can see two "woho" in the terminal, indicating that something is strange.
in the folder /myapp I have __init__.py:
from flask import Flask
app = Flask(__name__)
import myapp.views
and then in my views.py (also in /myapp) I have all the views like:
from myapp import app
from flask import render_template
#app.route('/')
def index():
return render_template('index.html')
it's due to the reloader of flask/werkzeug, which reloads automatically when you change the code.
so give debug=False if you don't want/need that, e.g. for "production".
How to stop Flask from initialising twice in Debug Mode?

Why doesn't vim print anything in console when running Python code?

I am trying to write some python code using tornado. Here is my code.
import sys
import tornado.ioloop
import tornado.web
import constants
class student():
name = ""
class MainHandler(tornado.web.RequestHandler):
def get(self):
loader = tornado.template.Loader(".")
print "MainiiiHandler"
self.write(loader.load("base.html").generate(pics=constants.pics))
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
So when i visit 127.0.0.1:8888, it should print MainiiiHandler in terminal. When i run python code with 'python test.py', it turns out actually like this. But when i run with :make in vim, it won't print MainiiiHandler. Because i really like the make function in vim, so can you help me solve this problem.
Check how
makeprg is python %
is written.
:set makeprg="python %"
does NOT work for me (echoes an empty string)
while
:set makeprg=python\ %
actually DOES work.
(if it doesn't help) This is what :h make shows:
The program given with the 'makeprg' option is started (default "make") with the optional [arguments] and the output is saved in the errorfile (for Unix it is also echoed on the screen).
If your system is not Unix, I suppose you have to supply the code that will print the contents of errorfile for you (don't know for sure as I tested it only under Linux).

Categories

Resources