Running two python file at the same time in visual studio code - python

i have two files, and i want to run both. The first one is basically asking down the prices of stocks and writes them to a database, and the second one using python dash to create a webpage. This webpage is showing the data in real time from the database which is constantly refreshes. I tried to use threading but it does not work, because the two files basically function as two infinite while loop. How should i solve this problem?
The two functions im having problem with are
dataMiningScript= fd.financeData(database,1600)
and
if name == "main":
app.run_server(debug=True)
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
database=db.dataBase()
homepage=hp.homepage(database)
homepage.timeUpdateCallback(app)
homepage.gaugeRefreshCallback(app)
dataMiningScript= fd.financeData(database,1600)
app.layout = homepage.layoutMaker()
if __name__ == "__main__":
app.run_server(debug=True)

You can just create two terminals in visual studio code to run both files (see here). Or you can create a simple shell script which starts both programs (start a program in the background with a '&' at the end of the command line)

For some reason i cannot run two python programs at the same time in Visual Studio Code, but i managed to solve this problem with the solution of the first commenter:
I opened terminal and search my .py program. Then i write
python3 xy.py

Helpful discussion here: run multiple python scripts at the same time.
Covers the question posted at the title fairly well.
On new window I managed to run the necessary code by typing a command. Run Python File button remains tied too original window.

Related

My .py file closes immediately after running without giving output

When I run a .py file, it just opens and closes immediately. It doesn't give any output. I turned it into an .exe file but still the same result. I ran it from cmd, the result is the same. I checked the program that runs the code and found it to be Python.
HERE IS THE CODE.
from tkinter import *
app = Tk()
app.geometry("250x250")
app.title("Tkinter!")
mainloop()
I suggest you to kindly share the code(as there may be some codes that does not have an output. For example, if the code simply involves assigning values to a variable(say x=2+4) will not result in printing an output, unless specifically mentioned otherwise(print(x)). Also, ensure that you add the Python's installed location to PATH(Environment Variables). I am assuming that you are using Windows OS(as you mentioned cmd).

Scripts overlapping while running in Pycharm

screenshot of my pycharm interface
Hey everyone. When I run this simple code (the tab 'youtubeyf.py
is to print 'hello world'), it produces the intended result but also another script (with the tab name "yfinance.py") result as well. In fact, the "yfinance.py" script doesn't need to be open in order for that result to appear too. It is almost as if it runs in the background, parallel without being open.
Goal: I want to run "print 'hello world'" without the dataframe from the other script appearing.
Problem: the dataframe is appearing without actually running or being open.
Troubleshoot attempts so far: I have "Alphabetted" and searched on StackOverflow, JetBrains on topics regarding reconfiguring the "run configurations", "parallels", and nothing yet.
Thank you for your time.
Edit 1: this does not solve my problem. for one, my issue isn't importing. in fact, everything is "running" smoothly, but that the results are two in one. It is analogous to two chefs preparing one meal each, say shrimp lo main and pizza, and then placing both meals onto one plate. I don't want that; I want to have shrimp lo main on Tuesday night, and then pizza Wednesday.
When importing yfinance, it could be possible that in that module, your code is structured such that when you import the module it runs a function or other code. In yfinance, you should delete any extraneous function that doesnt need to be run everytime you import yfinance somewhere else.
Edit: what you can do, if you dont want to change much of the structure yfinance.py, is wrap all the code that runs in that python file inside a main function and add the following:
if __name__=="__main__":
main()
This makes it so that python only runs main if you're actually running yfinance.py , and doesnt run main when you're importing it.

How to add systemtray to python script?

I want to add a systemtray to an existing python based project: https://github.com/piejanssens/premiumizer
How exactly do I need to do that? I am completely new to python and I am using it for the first time, just because I want to add a little feature to and existing script.
What I want to achieve is that when the script is running that there should be a system tray icon which opens http://localhost:5000 if it is double clicked. And if it is right clicked there should be an Exit/Quit option.
I have researched a bit and I think I could achieve it with one of these two scripts https://github.com/moses-palmer/pystray or with https://github.com/Infinidat/infi.systray (I also read that infi.systray should be used because it is not dependent on pywin32 because it uses the ctypes library because that one is part of the standard Python library).
So I tried to add this code for testing to the premiumizer.py file:
from infi.systray import SysTrayIcon
def say_hello(systray):
print "Hello, World!"
menu_options = (("Say Hello", None, say_hello),)
systray = SysTrayIcon("icon.ico", "Example tray icon", menu_options)
systray.start()
But now the Console is closing itself immediately. How can I check what went wrong? Is an error log saved somewhere ?
What do I need to do to make it work? Or is there an easier way for someone "stupid" like me ?
Welcome to the world of python!
Let me assume that you copied the script you've posted into a python file and just ran the file, correct? If so, the problem is that once the script is executed the program exits and with it the tray icon.
Start an interactive console by running python or (ipython if you have it installed) in a command window and paste in your code. You'll see that the tray icon appears and stays. It disappears once you close the console.
(Remark: the code above uses the python 2.x version of print without the () and will cause an error in python 3.x, there use print("Hello, World!").)
To make this work you need to put this code somewhere in the setup/initialization part of the premiumizer. Without knowing this project I cannot be of further help where to exactly.

Python file dialog freezing issue while using selenium

In order to print stuff on campus, I need to login to the webpage and submit the printing job manually on a browser, but I wanted to submit a job locally. So I wrote a small python script that uses Selenium to automate print job submission. I've verified that it works, but there's one thing that bugs me. Even after I select a file to print, the file dialog stays there until the actual code runs to the end.
The structure of the code is like the following.
1. Enter information on terminal (username, pw, which printer to use, how many copies, etc.).
2. Call tkinter.Tk().withdraw() to select a file (after selecting a file, "Submitting a print job..." is printed, as shown on the screenshot)
3. Do the actual Selenium job using information I collected above to submit a job
How can I make that file dialog disappear as soon as I select the file? Is it an ascyncio problem?
I noticed that many other people were experiencing a similar issue. While many had solved the issue by adding Tk().update() before or after askopenfilename() line, my problem was only gone when Tk().update() line was added both before and after the askopenfilename() line. FYI, I'm running the script on macOS with python 3.7.

How to launch Main Window in Visit (Visualiser) using Python?

I am trying to write a Python script that automatically draws out some plots and scatters on visit using my CFD results. I have the .visit file and it all works fine when I do it manually but I would like a python script to do it so I don't have to set everything up every time.
This is the code I wrote:
variable = ('nvrh','nvpr','nvtp') # define variables
OpenDatabase("runs/movie.visit") # open the data
for x in variable:
print "Drawing variable : ", x
AddPlot("Pseudocolor", x)
p = PseudocolorAttributes()
#p.colorTableName = "caleblack"
#SetPlotOptions(p)
DrawPlots()
and I run it on the terminal as:
visit -cli -s draw.py
which works just fine, but, the problem is it only creates a window and it fails to create the main window that you get when you normally launch visit. So i tried to launch it normally then add plots like so:
Launch()
variable = ('nvrh','nvpr','nvtp')
OpenDatabase("../runs/run2/movie.visit")
however when I do this, it launches an instant of visit then ignores the subsequent code and only when I close visit it does the same thing as before and launches only a single window.
What I am wondering is if there is a way of launching just the Main Window (the small menu thing) by the Python script.
Sorry if my question is a bit confusing and I am praying for experienced visit users as the community seems to be non-existent. Thanks for the help!

Categories