Make Jupyter Notebook script work 1 time per hour - python

I have a Jupyter Notebook. Here is just a simplified example.
#Parsing the website
def parse_website_function(url):
return(value,value2)
#Making some calculations(hypothesis)
def linear_model(value, value2):
return(calculations)
#Jot down calculations to csv file
pd.to_csv(calculations)
I would like to know how to make it work every hour and enable to rewrite(add new rows) to csv time series data in the same output file. Thanks!

A really basic way to do this would be to just make the program sleep for 3600 seconds.
For example this would make your program pause for 1 hour:
import time
time.sleep(3600)

Related

PyCharm runs a completely different code when i hit "run"

I wrote a code to open a CSV file with pandas
from pandas import read_csv
print(read_csv("weather_data.csv"))
But when I tried to run it in PyCharm, a completely different code that I wrote weeks ago gets executed.
I checked that I execute the right file, and even deleted the project multiple times and created it in different locations.
Another thing that confuses me is when I edit this code to:
print("hello")
from random import randint
from pandas import read_csv
print("hello")
print(randint(1,10))
print(read_csv("weather_data (1).csv"))
the output is:
hello
Pls enter a number: (-> the game i made weeks ago)
This code seems to get executed when I import packages, because the print function before works fine.

Running Python script in the background [duplicate]

This question already has an answer here:
Running Python Script as a Windows background process [duplicate]
(1 answer)
Closed 1 year ago.
I have created a Python script to change my background image based on the time of the day (Windows 10 user).
If the current time it's past the sunset time, then a specific image will be shown, if it's past the sunrise time, then there would be another one.
The sunrise/sunset data is being taken from a published source from an Excel spreadsheet.
What I want is to have this Python code running in the background instead of creating a Task Scheduler job to be ran every 30 seconds. Is there a better way to approach the code below in order to realize this?
from datetime import datetime
import pandas
import ctypes
file_path = "myfile.xlsx"
data = pandas.read_excel(file_path, header=0) #Column lines on line 0
#Today as day number
day = datetime.now().timetuple().tm_yday
#Today's parameters
sunrise = data["sr"][day-1] #sr is a column name in the Excel spreadsheet; Minus 1 to account for 0 based indexing;
sunset = data["ss"][day-1] #ss is a column name in the Excel spreadsheet; Minus 1 to account for 0 based indexing;
#Time right now
now = datetime.now().time()
#Setting up the day_night variable depending on the now variable
if now > sunrise and now < sunset:
day_night = 'day'
else:
day_night = 'night'
#The path to the wallpapers being used
path = 'C:\\wallpapers\\'+ day_night +'.jpg'
SPI_SETDESKWALLPAPER = 20
#Function to change the wallpaper
def changeBG(path):
ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, path, 3)
changeBG(path)
Sorry in advance for the messy code. Yesterday was my first day writing code for this kind of purposes.
you can write
pythonw.exe code.py
this will run the code in the background and you will need to turn it off from task manager
if you want this program to start when the computer starts you can place a batch file in shell:startup and write there
pythonw.exe C:\path\To\Code.py
and that's it

How can I prevent my Python web-scraper from stopping?

Ahoy! I've written a quick (Python) program that grabs the occupancy of a climbing gym every five minutes for later analysis. I'd like it to run non-stop, but I've noticed that after a couple hours pass, one of two things will happen.
It will detect a keyboard interrupt (which I did not enter) and stop, or
It will simply stop writing to the .csv file without showing any failure in the shell.
Here is the code:
import os
os.chdir('~/Documents/Other/g1_capacity') #ensure program runs in correct directory if opened elsewhere
import requests
import time
from datetime import datetime
import numpy as np
import csv
def get_count():
url = 'https://portal.rockgympro.com/portal/public/b01ab221559163c5e9a73e078fe565aa/occupancy?&iframeid=occupancyCounter&fId='
text = requests.get(url).text
line = ""
for item in text.split("\n"):
if "\'count\'" in item:
line = (item.strip())
count = int(line.split(":")[1][0:-1]) #really gross way to get count number for this specific source
return count
while True: #run until manual stop
with open('g1_occupancy.csv', mode='a') as occupancy:
occupancy_writer = csv.writer(occupancy)
occupancy_writer.writerow([datetime.now(), get_count()]) #append new line to .csv with timestamp and current count
time.sleep(60 * 5) #wait five minutes before adding new line
I am new to web scraping (in fact, this is my first time) and I'm wondering if anyone might have a suggestion to help eliminate the issue I described above. Many thanks!

How to automatically import csv file(from local pc) every minute (synchronize it with my computer clock)

I have a csv file in my computer that updates automatically after every 1 minute eg. after 08:01(it updates), after 08:02(it updates) etc...
importing this file to python is easy...
import pandas as pd
myfile=pd.read_csv(r'C:\Users\HP\Desktop\levels.csv')
i want to update/re-import this file after every minute based on my pc clock/time. i want to use 'threading' since i want to run other cells while the import function is running at all times.
so basically the code might be(other suggestions are welcome):
import pandas as pd
import threading
import datetime
import time
# code to import the csv file based on pc clock automatically after every
minute.
i want this to run in a way that i can still run other functions in other cells(i tried using "schedule" but i cant run other functions after that since it shows the asterisk symbol(*))
meaning if i run on another cell the variable 'myfile'
myfile
it shows a dataframe with updated values each time.

Displaying time and date on html file using python

I am trying to use import time, and strftime(), to import time, then use the index.open() to write the date on a html file, this is not working , is there any way to display time on html for every 10 mins for example ?

Categories