using flask in spyder stuck in local server - python

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.

Related

How to run a Django project on Ubuntu in AWS EC2?

I have created a Django application and uploaded in to AWS EC2. I can access the site using public IP address only when I run the python manage.py in AWS command line.
If I close the Putty window, I am not able to access the site. How can I make sure that the site is available always even if I close the command line / putty?
I tried WSGI option but its not working at all. Appreciate your help to give us a solution to run the Python application in AWS.
It happens because you are running the app from within the SSH session, which means that ending the session (SIGHUP) will kill your application.
There are several ways to keep the app running after you disconnect the SSH, the simplest would be to run it inside a screen session and keeping this instance running while disconnecting from SSH, the advantage of this method is that you can still control the app when you are reconnecting to this machine and control the state of the app and also potentially see the logs.
Although it might be pretty cool it's considered a patch, the more stable and solid way would be to create a service that will run the app and will allow you to start, stop and look at logs using the nifty wrappers of systemd.
Keep the process running with screen:
First you'll have to make sure screen is installed (apt-get or yum) whatever suits your desired distro.
Run screen.
Run the app just like you did outside screen.
Detach from the screen session by pressing Ctrl+A and then d.
Disconnect from the SSH and see how the service is still running.
Creating a systemd service is a bit more complicated so try and read through the following manual.

Running Python script as Executable

I have been attempting to run my python script as a service, and have followed the advice contained in several previous forum posts. However, these have not helped me thus far. Here is what I have attempted up until now:
Used an SMWinservice class that allowed me to install my Python script as a service. This solution, however, would not launch. If I would try to start it, I would receive an error message.
I have tried using NSSM with : nssm install myService pathToInterpreter PathToScript. Note, all files etc. can be found in the local directory of the PathToScript. For what its worth, I also tried using the GUI version of NSSM. When I rebooted my computer, it showed as "Paused" in task manager. I stopped it, and then tried to run it again, and I received an error. I have tried NSSM with both python.exe and pythonw.exe.
I suspect that a possible source of error is the fact that my program uses a text file as a configuration file. This configuration file has been coded as being in my local working directory. However, I wouldn't think this would be an issue with NSSM. I know for a fact that my program will completely exit, using the exit command, if the configuration file is not found.
I was thinking of doing a batch file and starting the batch file with the script and running it like that, but I prefer a service since it can monitor the process, can restart it, or windows can notify me via email if there is an issue with my service.
For completeness, I should also mention that the program runs without issue outside of a service. For the reason that the program runs as expected, I decided to not post the code, unless someone would like to see it for whatever reason.

How to hide the console when running the django developement server?

I want to run the django developement server localy and I don't want to use a full webserver.
So far I renamed manage.py to manage.pyw. Then in manage.pyw I call execute_from_command_line(['manage.py', 'runserver'])
In a batch file I then use "START pythonw manage.pyw"
The problem is, that I then cannot connect to the server even though in the taskmanager it says it's still running. When I start it using python instead, it runs, but the console window is visible.
I found a way to do this. I used the Windows Service Wrapper https://github.com/kohsuke/winsw to install python manage.py runserver as a service.

Pelican restarting you server

Hi I just started working with Pelican and it really suits my needs, I had tried to build blogs in Flask and other frameworks but I really just wanted something simple so I can post about math, and pelican just works.
My question is when I am testing on my machine, I start the server; however when I stop the server to make some edits to my test blogs, and then try to reload the server I get a socket already in use error. I am stopping my server by ctrl+z am I doing this correctly?
Use ctrl+c to terminate the process. ctrl+z will only send it's execution to the background.
On a separate note, since you are making changes and want to test them it would be more convenient to use $ make devserver instead of $ make serve. See docs.
For your development server, you can also use the script ./develop_server.sh that comes with the last versions of pelican (at least with the 3.5.0).
Build the blog and load a server with ./develop_server.sh start: it reloads each time you edit your blog (except the settings). Simply stop with ./develop_server.sh stop when you're finished.
When you press Ctrl + C or Ctrl + z, do not restart the HTTP server: it is running in the background, and that is the exact reason as why you are getting that error message.
To see that the server is running in the background after pressing the any of the key combinations above, try to edit and save any file: you will see right away in the terminal the re-generation process of your pages is active again.
You can start the HTTP server using this command: make devserver and stop by ./developer_server.sh stop

to keep the script running even after internet connection goes off

I had putty on one server and run a python script available on that server. That script keep on throwing output on terminal. Later on, my internet connection went off but even then i was expecting my script to complete it job as script is on running on that server. But when internet connection resumed, I found that script has not done its job.
So is this expected ? If yes, then what to do to make sure that script runs on server even though internet connection goes off in-between?
Thanks in advance !!!
You should use screen which will let you "detach" your process from the actual terminal you're in.
On the server, you can install tmux or screen. These programs run the program in the background and enable you to open a 'window', If I use tmux:
Open tmux: tmux
Detach (run in background): press Ctrl-b d
reattach (open a 'window'): tmux attach

Categories