Abstraction for managing the scheduling of Python scripts through CronJobs with a out-of-the-box Streamlit Panel.
pip install python_scsIf you want to use the Streamlit panel:
pip install python_scs[streamlit]Check out the full example.
- Make sure you have the following directory structure:
├── scripts/
│ ├── logs/
│ ├── script_test.py
│ ├── __init__.py
├── streamlit_pannel.py
- Instantiate the script manager and configure the panel:
# streamlit_pannel.pyfrompython_scsimportPythonScriptsCronManager, streamlit_uimanager=PythonScriptsCronManager(
user=True
)
streamlit_pannel=streamlit_ui.init(
manager,
layout='wide',
title='Scripts Manager',
subheader='Manage your python scripts'
)- Run the panel using
streamlit:
streamlit run streamlit_pannel.pyimportosfrompython_scsimportPythonScriptsCronManagerscripts_manager=PythonScriptsCronManager(
config=PythonScriptsCronManager.Config(
app_path=os.path.abspath("."), # Root directory where scripts_folder is locatedscripts_folder="scripts", # Directory containing scriptslogs_folder="scripts/logs"# Directory for logs
),
user=True
)📌 Check the python-crontab documentation to understand the user parameter.
scripts=scripts_manager.get_scripts()
print(scripts) # ["script_test.py"]job=scripts_manager.set_script_job(
script_name="script_test.py",
schedule=["* * * * *"],
comment="Test schedule",
enable=True
)
# Creating a schedule with a custom UNIX commandjob=scripts_manager.set_job(
command='echo "Teste"',
schedule=["* * * * *"],
log_file_name="test.txt", # Required to store outputcomment="Custom schedule",
enable=True
)📌 To verify if the schedule was created, run:
crontab -ljobs=scripts_manager.get_jobs()
forjobinjobs:
print(f"{job.comment} - {job.script_name} - {job.is_runing()}")
# Fetching a scheduled job by filtersjob_script_test=scripts_manager.get_job({
"script_name": "script_test.py",
"comment": "Test schedule"
})job=scripts_manager.get_job({
"script_name": "script_test.py",
"comment": "Test schedule"
})
job.enable_job() # Enables the jobjob.disable_job() # Disables the jobjob.toggle_job() # Toggles between enabled/disabled# Manually execute the scriptscripts_manager.execute(job)
# Execute as a subprocessscripts_manager.execute(job, use_subprocess=True)
# Remove the schedulescripts_manager.remove_job(job)This project is distributed under the MIT license. See the LICENSE file for more details.