Skip to content

Abstração com Painel em Streamlit out-of-the-box para gestão do agendamento de scripts em Python através de CronJobs.

License

Notifications You must be signed in to change notification settings

iagobalmeida/python_scs

Repository files navigation

Python Scripts Cron Scheduler

Abstraction for managing the scheduling of Python scripts through CronJobs with a out-of-the-box Streamlit Panel.

Python Linux Streamlit

Tests Status Coverage Status

📌 Installation

pip install python_scs

If you want to use the Streamlit panel:

pip install python_scs[streamlit]

🚀 Configuration

Check out the full example.

  1. Make sure you have the following directory structure:
├── scripts/
│   ├── logs/
│   ├── script_test.py
│   ├── __init__.py
├── streamlit_pannel.py
  1. 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'
)
  1. 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.

About

Abstração com Painel em Streamlit out-of-the-box para gestão do agendamento de scripts em Python através de CronJobs.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •