VS Code not identifying any changes made to the code: - python

Any changes I make to my code aren't doing anything. I am using three things html, flask and python. Any code change I make to my python or html code is not getting implemented on the browser when I am trying to run it.IT is only running older version of the code.
How can I rectify this error of vscode?

Make sure you're refreshing the page if you're opening a HTML file. If you're accessing the python server make sure to restart it every time you make changes to the code. The way to do it is ctrl+c in the terminal and running the command again. There are ways to make this happen automatically, refer here for more information

Did you start your flask server in debug mode?
from Python
app.run(debug=True)
or from terminal
$ export FLASK_DEBUG=1
$ flask run

Try to run in incognito once. Or try after clearing cache.

Related

How can I open a testing server for flask?

I am creating a blog using Flask. However, when trying to run my code on a local server, I am unable to do so, as it comes with this error:
Error: Failed to find Flask application or factory in module "flaskblog". Use "FLASK_APP=flaskblog:name to specify one.
I had typed in, "set FLASK_APP=flaskblog.py" in my terminal prior, then typed in "flask run." What would be the next best steps to take so I can run the code on a local server? Running this on a Windows 10 computer.
If you're using a bash cli, try using "export FLASK_APP=flaskblog.py". Set might only work if you are using something like Windows CMD.

Stop Python localhost server on every port

Since I tried a tutorial with Python / Django using venv etc, i'm blocked with a localhost server that shows folder of this project:
I use Visual Studio Code and I use LiveServer configured on port 0. Each time I want to start LiveServer, it renders me this page and nothing else.
Have you any idea why and how to stop it?
I modified my PATH folder to make the Python's project work. Is it the problem?
Thank you
The way to fix this is by going to VS Code >> File >> Open Folder, and the select the folder with your html file in it then you can run live server properly. However, if you want to stop live server simply closing the web browser it's running on will do.

Python Script Nor Running In PythonAnywhere

I am working on a Django project. It includes a functionality in which when admin clicks on a certain button, it runs a Python script and redirect it to some other page after complete process. Before hosting, it was working fine. Now that I hosted it here, the script doesn't run at all. It just redirects to another page without providing any error. Why's that? Also, my script includes chromedriver, in case it has anything to do with it.
Kindly help me.
This might be the error. Why doesn't it recognize it?
Code
out = run([sys.executable, '//home//maisum279//traffic//traffic//trafficapp//traffic.py', "--domain", url, "--threads", '5', "--max-clicks", maximum, "--min-clicks", minimum, "--stay", stay, "--requests", requests])
Server Log
2020-09-21 11:05:02 /usr/local/bin/uwsgi: unrecognized option '--domain'
2020-09-21 11:05:02 getopt_long() error
The script is in Python, but now it seems to be running with uswgi. What can I do make it work? Please help.
Since PythonAnywhere is apparently running things with uWSGI, which embeds Python, sys.executable is uwsgi and not python3.
You may want to try e.g. ['/usr/bin/env', 'python3', ...] instead of relying on sys.executable.

pycharm and flask autoreload and breakpoints not working

I'm using Pycharm 4, with flask 0.10.1, python 3.4
It seems that when running a flask application from inside pycharm, if I run it with:
app.run(debug=True)
My breakpoints are ignored. After some googling, I've found that in order to make PyCharm stop on breakpoints, I should run flask with:
app.run(debug=True, use_reloader=False)
Now PyCharm correctly stops on breakpoints, but I miss the autoreloading feature.
Is there any way to make both work together?
Using python 2.7 both things work
I reported this to PyCharm: https://youtrack.jetbrains.com/issue/PY-13976
I'm going to start with the short answer: No, what you want cannot be done with any releases of PyCharm up to 4.0.1.
The problem is that when you use the reloader the Flask application runs in a child process, so the PyCharm debugger is attached to the master process and has no control over the child.
The best way to solve this problem, in my opinion, is to ask Jetbrains to build a "restart on change" feature in their IDE. Then you don't need to use Werkzeug's reloader at all and you get the same functionality direct from PyCharm.
Until Jetbrains decides to implement this, I can share my workaround, which is not terribly bad.
In the "Edit Configurations", set the configuration you are going to use to "Single Instance only" (check box in the top right of the dialog box)
Make sure the configuration is the active one.
Configure your Flask app to not use the Werkzeug reloader.
Press Ctrl-D to start debugging (on Mac, others may have a different shortcut)
Breakpoints should work just fine because the reloader isn't active.
Make any code changes you need.
When you are ready to restart, hit Ctrl-D again. The first time you do it you will get a confirmation prompt, something like "stop and restart?". Say yes, and check the "do not show again" checkbox.
Now you can hit Ctrl-D to quickly restart the debugger whenever you need to.
I agree it is not perfect, but once the Ctrl-D gets into your muscle memory you will not even think about it.
Good luck!
I found that in PyCharm 2018.1.2 there is FLASK_DEBUG checbox in run configuration:
With this after making some changes, saving file triggers reload action.
In my setup, I'm debugging the flask app by running a main.py file which sets some configuration and calls app.run(). My python interpreter is set up in a Docker container.
My issue was that I needed to check Run with Python console.
The problem is because with use_reloader=True the werkzeug application is started in a seperate (child) thread of main application and PyCharm fails to correctly handle breakpoints because they are lost when the thread starts.
You can try to follow this thread: http://forum.jetbrains.com/thread/PyCharm-776 but it seams there was not too much progress on that.
I'd suggest using something Python-ish like pdb, i.e.:
#app.route('/<string:page>')
def main(page):
import pdb; pdb.set_trace() # This line actually stops application execution
# and starts Python debug shell in the console
# where you can examine current scope and continue
# normal code execution at any time.
# You can inject *any* code here.
# For example, if you type `print page` during pause,
# it will output content of "page" variable.
return render_template('index.html')
Try configuring this python running configuration in "Edit Configurations". After that, run in debug mode.
You need to unlock the console.
you start the app in debug mode
then you make something that causes an error.
at the end of the error message from flask is this
Here you enter the PIN flask prints in the console at the start
copy paste this pin into the console and click confirm PIN
now the breakpoints will work
from pycharm 2017 using python 2.7 (in my case with virtual env, but I suppose not necessary) I do:
run...
leave scripts and scripts parameters blank
I put in interpreter options: -m flask run
set the env variables FLASK_APP
than run attach to local process, and finally choose the running process
my use case is to connect from postman to flask rest services endpoints and interrupt on my breakpoints

Localhost has stopped updating when various flask/python scripts are run, how do I fix this?

I've been testing out a few .py files with Flask, referring to 127.0.0.1:5000 frequently to see if they're interfacing correctly with the HTML, after running the .py I'm getting the following as normal:
* Running on http://127.0.0.1:5000/
* Restarting with reloader
However, 127.0.0.1:5000 has suddenly stopped updating when scripts are run, remaining as it was after the first time a script was run since my computer has been turned on (restarting the machine is the only way I've found to take a fresh look at my work). To confirm that it's not an issue within .py files or my templates, Flask's hello world example with app.run(debug=True) does not update this localhost page when run. Is there any way to remedy this?
Two things that may or may not be involved with this issue:
(1) I am not using virtualenv but simply running .py files from folder directories on my desktop (following proper format for Flask and the template engine, though). (2) Around the time the problem started I installed SQLAlchemy and its Flash extension, Flask-SQLAlchemy with pip.
After tracking the processes down by running $ netstat -a -o in the command line, it turns out it wasn't a code error, but rather multiple instances of pythonw.exe, which can be taken care of in the Task Manager. I'm not sure why the processes keep running after I close out of all python windows, or why it keeps communicating with 127.0.0.1:5000, however, so thoughts on this would still be appreciated.
Thats right. Just press 'Ctrl+Shift+Esc' to open the task manager.
Scroll down to find the 'python3.exe' files and end the task manually.
The reason is 'ctrl+c' doesnt work for me (it just copies the text on terminal window), so I have to manually kill the python interpreter running in the background. Its hard work, but hey atleast you dont have to restart your computer everytime!!

Categories

Resources