I am developing a Streamlit app and when I run it, it will be launched on http://localhost:8501.
Now, I have a particular page in my app and when a user navigates to it, a process will be executed. The process is executed by running another script in my app called run.py, which is a typically executed via the command line by passing in some arguments.
This script starts up a uvicorn server with a FastAPI app. It basically displays a visualization of sorts. The server runs on http://127.0.0.1:4141.
What I want to happen is this: when the user navigates to this particular page, the process must run and once it is done, the server URL (http://127.0.0.1:4141) must be launched (preferably in a separate tab), while my Streamlit app is still running.
How do I do this?
The code in my Streamlit page looks like this,
import subprocess
import webbrowser
subprocess.run("python run.py -arg1=val1 -arg2=val2")
# this line never executes because the server is launched from the previous line
webbrowser.open('http://127.0.0.1:4141')
How can I refactor this to achieve what I want? Should I be running my first line as a background process or something? Is the webbrowser package the right way to go here?
Related
I am trying to have WebCORE (smartthings) run a Get request to run a node.js script EufyNodeClient and change the security mode for my cameras.
I have successfully installed the eufynode script and am able to execute the .js file from the terminal.
I successfully used a WebCORE piston using flask and an executable python script to run the node. With Flask installed, when I start Flask in the terminal, flask starts and the script runs but I cannot make another request via HTTP Get request.
I can also repeatedly run the Flask and python script using Thonny Python IDE with or without starting the Flask server in the terminal.
I am a super noobie and have been able to get this far with searching and reading but now I'm stuck.
Is there a way to have Flask in the background and still accept an incoming request to change the security mode? This setup is just for me and would stay inside my network so if I don't need a production server, that would be great.
Here is my Flask app:
from flask import Flask
from home import subprocess
app = Flask(__name__)
#app.route('/home/pi/eufy-node-client-examples/')
def mode_setting():
return home.mode_subprocess()
if __name__ == "__main__" :
app.run()
and python script
#!/usr/bin/python
from subprocess import run
run(["node", "examples/p2p-local/index.js"])
I am looking for help deploying my flash app. I've already written the app and it works well. I'm currently using the following command in the directory of my flask code:
sudo uwsgi --socket 0.0.0.0:70 --protocol=http -w AppName:app --buffer-size=32768
This is on my Amazon Lightsail instance. I have the instance linked to a static public IP, and if I navigate to the website, it works great. However, to get the command to continuously run in the background even after logging out of the Lightsail, I first start a screen command, execute the above line of code, and then detach the screen using ctrl-a-d.
The problem is, if the app crashes (which is understandable since it is very large and under development), or if the command is left running for too long, the process is killed, and it is no longer being served.
I am looking for a better method of deploying a flask app on Amazon Lightsail so that it will redeploy the app in the event of a crash without any interaction from myself.
Generally you would write your own unit file for systemd to keep your application running, auto restart when it crashes and start when you boot your instances.
There are many tutorials out there showing how to write such a unit file. Some examples:
Systemd: Service File Examples
Creating a Linux service with systemd
How to write startup script for Systemd?
You can use pm2
Starting an application with PM2 is straightforward. It will auto
discover the interpreter to run your application depending on the
script extension. This can be configurable via the Ecosystem config
file, as I will show you later on this article.
All you need to install pm2 and then
pm2 start appy.py
Great, this application will now run forever, meaning that if the process exit or throw an exception it will get automatically restarted. If you exit the console and connect again you will still be able to check the application state.
To list application managed by PM2 run:
pm2 ls
You can also check logs
pm2 logs
Keeping Processes Alive at Server Reboot
If you want to keep your application online across unexpected (or expected) server restart, you will want to setup init script to tell your system to boot PM2 and your applications.
It’s really simple with PM2, just run this command (without sudo):
pm2 startup
Pm2 Manage-Python-Processes
I am trying to create a web application using flask. I have already gotten somewhat comfortable with using python, and have done so using spyder, inside of Anacanda Navigator. Now I am playing around with flask doing basic functions and have successful so far by testing it out in local server 127.0.0.1:5000. The problem I am having is that I cannot stop the server once I run the script in spyder. I have stopped the script and run other scripts through the console, but the local server remains the same.
The reason this is a problem for me is because when I try to change files and run a different flask script, the server does not update with the new information. For example, if I run a flask script that returns "Hello World" on the main page, and then I stop that file, open a new file that has a different flask script that returns "The sky is blue" the server does not change when I check it on chrome or any other browser. It will onyl return "Hello World"
I have been able to fix this problem by completely restarting my computer, but I am wondering if there is another way, just to restart the local server, 127.0.0.1:5000. Thank You!
Also I am using windows
I do : "Run > Configuration per file > Execute in an external system terminal",
then when you run your .py containing the app.run, it will be launched in an external console. If you close the console the server will be closed too.
To Kill the local server, you may use Ctrl+C command and not any other command. This command is also mentioned when the server is up and running.
I've been having this precise issue and have been smashing my head against the wall for a couple of hours. I posted the referenced StackOverflow question (my first actually) and it seems that running a script from inside Spyder is the wrong way to go as it leaves runaway background processes running, even after restarting Spyder.
I got the recommendation to only launch my *.py code from the command prompt. Furthermore I was told to do this:
set FLASK_APP=main1.py then set FLASK_DEBUG=1 then flask run
though I'm not sure what that does, so I will investigate. I was about to restart my computer as a last ditch effort until I looked in my Windows Task Manager and found some Python tasks running. After [end task] them both I was able to launch the updated webpage on my local host.
I have a flask web server running on the cloud and the process goes down if I close the terminal or if my local machine loses network connection. So how can I make sure that my flask process runs forever with out any dependency on the terminal? And is there any way to monitor the process. Like for example if I log in again and go to a URL I get all the details I need.
You can use screen command. Let's assume that your application is app.py and the steps are as follows.
1.Create a new session and it will write screen output to a file named screenlog.0.
screen -L
2.Now you are in a session and you can run your application.
python app.py
3.Detach this session.
Press "Ctrl-A" and then press "D" on your keyboard to detach the session.
Then you will return your normal terminal. Your application is running in the background in a virtual screen and all screen output will be written to the file screenlog.0. You can close your terminal without affecting your application.
4.Reattach to the previous session.
If you want to shut down your application or to do some other things, you can use command screen -r to reattach to the previous session.
5.Terminate a session.
After attaching to a session, just use exit to terminate it.
For more details, please read this tutorial.
I have web application on Flask and user can send some request which my script is processing and then, running another script in python in the console with some parameters like this:
import sys
os.system('python start.py -u 100 -p 122224')
All works good, but now I want controlling all running copies of my script like start, stop and pause.
How i can do this without crutches?
Check subprocess and multiprocessing modules. The first one allows you to execute external application. To use the second one, you'll be required to call some python code, but the management capabilities should be much wider.