Agendador de Scripts Python usando Crontab com Painel Streamlit
Project description
Python Scripts Cron Scheduler
Abstraction for managing the scheduling of Python scripts through CronJobs with a out-of-the-box Streamlit Panel.
📌 Installation
pip install python_scs
If you want to use the Streamlit panel:
pip install python_scs[streamlit]
🚀 Configuration
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.py
from python_scs import PythonScriptsCronManager, streamlit_ui
manager = 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.py
🛠 Using the API
Instantiating the script manager
import os
from python_scs import PythonScriptsCronManager
scripts_manager = PythonScriptsCronManager(
config=PythonScriptsCronManager.Config(
app_path=os.path.abspath("."), # Root directory where scripts_folder is located
scripts_folder="scripts", # Directory containing scripts
logs_folder="scripts/logs" # Directory for logs
),
user=True
)
📌 Check the python-crontab documentation to understand the user parameter.
Listing available scripts
scripts = scripts_manager.get_scripts()
print(scripts) # ["script_test.py"]
Creating a schedule
job = scripts_manager.set_script_job(
script_name="script_test.py",
schedule=["* * * * *"],
comment="Test schedule",
enable=True
)
# Creating a schedule with a custom UNIX command
job = scripts_manager.set_job(
command='echo "Teste"',
schedule=["* * * * *"],
log_file_name="test.txt", # Required to store output
comment="Custom schedule",
enable=True
)
📌 To verify if the schedule was created, run:
crontab -l
Listing configured schedules
jobs = scripts_manager.get_jobs()
for job in jobs:
print(f"{job.comment} - {job.script_name} - {job.is_runing()}")
# Fetching a scheduled job by filters
job_script_test = scripts_manager.get_job({
"script_name": "script_test.py",
"comment": "Test schedule"
})
Enabling, disabling, executing, and removing a scheduled job
job = scripts_manager.get_job({
"script_name": "script_test.py",
"comment": "Test schedule"
})
job.enable_job() # Enables the job
job.disable_job() # Disables the job
job.toggle_job() # Toggles between enabled/disabled
# Manually execute the script
scripts_manager.execute(job)
# Execute as a subprocess
scripts_manager.execute(job, use_subprocess=True)
# Remove the schedule
scripts_manager.remove_job(job)
📜 License
This project is distributed under the MIT license. See the LICENSE file for more details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_scs-1.2.tar.gz.
File metadata
- Download URL: python_scs-1.2.tar.gz
- Upload date:
- Size: 10.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3a17d6ac3895f51d741e1c4d6bef0736dddf07f0e93f0f55bd6e5ce13da5090d
|
|
| MD5 |
0669a490c43a5daaf4c6f37d1d860cce
|
|
| BLAKE2b-256 |
afa903f7263c7d1b4e423767d093b8a96add26283b0e006ce5c02a02a97db146
|
File details
Details for the file python_scs-1.2-py3-none-any.whl.
File metadata
- Download URL: python_scs-1.2-py3-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e05e96a6e05b56aca1a8484c7a366554d4c4b825773ea244aefb90b307871510
|
|
| MD5 |
e13a69ebc78264f62398d1cafc89ccf0
|
|
| BLAKE2b-256 |
b67740a0f27a163fcf58b0d28ddb067e817fdaff0f5d936eb93c8016f6d340f6
|