django-oemof


Namedjango-oemof JSON
Version 0.19.0 PyPI version JSON
download
home_pageNone
SummaryDjango application to run oemof
upload_time2025-01-30 11:00:33
maintainerNone
docs_urlNone
authorHendrik Huyskens
requires_python<4.0,>=3.9
licenseAGPL-3.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            from django_oemof.tests.test_oemof_parameters import OEMOF_DATAPACKAGE

# Django-Oemof

Django-Oemof is a Django app to provide an API to build and optimize oemof.solph models and deliver results via JSON response.

## Requirements

- `oemof.tabular` has to be installed 
- CBC solver has to be installed. Install it via (conda):
```
conda install -c conda-forge coincbc
```

Django project must use celery and automatically detect celery tasks. (follow https://docs.celeryq.dev/en/stable/django/first-steps-with-django.html to setup celery)

## Quick start

1. Add "oemof" to your INSTALLED_APPS setting like this::
    ```
        INSTALLED_APPS = [
            ...
            'rest_framework'
            'django_oemof',
        ]
    ```

2. Include the oemof URLconf in your project urls.py like this::

    path('oemof/', include('django_oemof.urls')),

3. Run ``python manage.py migrate`` to create the oemof models.

## Configuration

You can set following configs via environment:

- DJANGO_OEMOF_IGNORE_SIMULATION_PARAMETERS
  list of parameter keys which shall be ignored when initializing a simulation 

## OEMOF Datapackages

Have to be put in folder `oemof` within djangos `MEDIA_ROOT` folder.
Name of datapackage folder is used in request for building ES.

## Hooks

Hooks can be used to change default behaviour of parameter setup, energysystem build and model solving.
This is done by defining custom functions which can be registered in django_oemof and are applied when simulating an ES.
Depending on hook type (Parameter/Energysystem/Model), the defined custom functions are applied to parameters, build Es or after creating the model.
See following flow chart for order of hooks:

![Hook Flow Chart](./docs/images/oemof_flow.png)

Every hook is scenario-dependent to allow different hooks per scenario, but you can use `hooks.ALL_SCENARIO` as scenario to aplly hook to all scenarios.
An example hook (changing default behaviour of parameter setup) could be set up as follows:

```python

from django_oemof import hooks


def converting_demand_to_kW(data):
   data["demand"] = data["demand"] * 1000
   return data


demand_kW_hook = hooks.Hook(scenario="dispatch", function=converting_demand_to_kW)
hooks.register_hook(hooks.HookType.PARAMETER, demand_kW_hook)

```

## Tests

Run tests for standalone app via `python runtests.py`

## Standalone

This section is about using django-oemof without necessity to set up a django webserver.
You can store/restore simulated oemof.tabular datapackages using djangos ORM.
Additionally, hooks from django-oemof to 
- change parameters before simulation,
- change ES after building from datapackage or 
- changing model before simulating 

are available.

### Usage

Steps to run simulation:
1. Set up database url as `DATABASE_URL` in `.env` file in working directory
2. Migrate django models via `python -m django_oemof.standalone migrate`
3. Download or create a valid oemof.tabular datapackage and store it in folder `media/oemof`
   (Media folder can be changed via `MEDIA_ROOT` in `.env` file)
4. Create script which imports `init_django` from `django_oemof.standalone` 
5. Now, you can save/restore oemof results to/from DB using:
```python
# Example with some hooks
from django_oemof import simulation, hooks

OEMOF_DATAPACKAGE = "dispatch"

# Hook functions must be defined beforehand
ph = hooks.Hook(OEMOF_DATAPACKAGE, test_parameter_hook)
esh = hooks.Hook(OEMOF_DATAPACKAGE, test_es_hook)
mh = hooks.Hook(OEMOF_DATAPACKAGE, test_model_hook)

hooks.register_hook(hook_type=hooks.HookType.PARAMETER, hook=ph)
hooks.register_hook(hook_type=hooks.HookType.ENERGYSYSTEM, hook=esh)
hooks.register_hook(hook_type=hooks.HookType.MODEL, hook=mh)

parameters = {}
simulation_id = simulation.simulate_scenario(scenario=OEMOF_DATAPACKAGE, parameters=parameters)
print("Simulation ID:", simulation_id)

# Restore oemof results from DB
from django_oemof import models
sim = models.Simulation.objects.get(id=1)
inputs, outputs = sim.dataset.restore_results()
```
   
*Note*: `django_oemof.models` must be loaded *AFTER* `init_django()` call. 
Thus, import of `django.models` might look unusual and linter might complain - 
but otherwise django models are not ready yet and a django error will occur! 


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "django-oemof",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.9",
    "maintainer_email": null,
    "keywords": null,
    "author": "Hendrik Huyskens",
    "author_email": "hendrik.huyskens@rl-institut.de",
    "download_url": "https://files.pythonhosted.org/packages/a4/a9/da31d5ef02f54b508d36126dd2ef6cac4ba68f38640ecad3897622ffcc98/django_oemof-0.19.0.tar.gz",
    "platform": null,
    "description": "from django_oemof.tests.test_oemof_parameters import OEMOF_DATAPACKAGE\n\n# Django-Oemof\n\nDjango-Oemof is a Django app to provide an API to build and optimize oemof.solph models and deliver results via JSON response.\n\n## Requirements\n\n- `oemof.tabular` has to be installed \n- CBC solver has to be installed. Install it via (conda):\n```\nconda install -c conda-forge coincbc\n```\n\nDjango project must use celery and automatically detect celery tasks. (follow https://docs.celeryq.dev/en/stable/django/first-steps-with-django.html to setup celery)\n\n## Quick start\n\n1. Add \"oemof\" to your INSTALLED_APPS setting like this::\n    ```\n        INSTALLED_APPS = [\n            ...\n            'rest_framework'\n            'django_oemof',\n        ]\n    ```\n\n2. Include the oemof URLconf in your project urls.py like this::\n\n    path('oemof/', include('django_oemof.urls')),\n\n3. Run ``python manage.py migrate`` to create the oemof models.\n\n## Configuration\n\nYou can set following configs via environment:\n\n- DJANGO_OEMOF_IGNORE_SIMULATION_PARAMETERS\n  list of parameter keys which shall be ignored when initializing a simulation \n\n## OEMOF Datapackages\n\nHave to be put in folder `oemof` within djangos `MEDIA_ROOT` folder.\nName of datapackage folder is used in request for building ES.\n\n## Hooks\n\nHooks can be used to change default behaviour of parameter setup, energysystem build and model solving.\nThis is done by defining custom functions which can be registered in django_oemof and are applied when simulating an ES.\nDepending on hook type (Parameter/Energysystem/Model), the defined custom functions are applied to parameters, build Es or after creating the model.\nSee following flow chart for order of hooks:\n\n![Hook Flow Chart](./docs/images/oemof_flow.png)\n\nEvery hook is scenario-dependent to allow different hooks per scenario, but you can use `hooks.ALL_SCENARIO` as scenario to aplly hook to all scenarios.\nAn example hook (changing default behaviour of parameter setup) could be set up as follows:\n\n```python\n\nfrom django_oemof import hooks\n\n\ndef converting_demand_to_kW(data):\n   data[\"demand\"] = data[\"demand\"] * 1000\n   return data\n\n\ndemand_kW_hook = hooks.Hook(scenario=\"dispatch\", function=converting_demand_to_kW)\nhooks.register_hook(hooks.HookType.PARAMETER, demand_kW_hook)\n\n```\n\n## Tests\n\nRun tests for standalone app via `python runtests.py`\n\n## Standalone\n\nThis section is about using django-oemof without necessity to set up a django webserver.\nYou can store/restore simulated oemof.tabular datapackages using djangos ORM.\nAdditionally, hooks from django-oemof to \n- change parameters before simulation,\n- change ES after building from datapackage or \n- changing model before simulating \n\nare available.\n\n### Usage\n\nSteps to run simulation:\n1. Set up database url as `DATABASE_URL` in `.env` file in working directory\n2. Migrate django models via `python -m django_oemof.standalone migrate`\n3. Download or create a valid oemof.tabular datapackage and store it in folder `media/oemof`\n   (Media folder can be changed via `MEDIA_ROOT` in `.env` file)\n4. Create script which imports `init_django` from `django_oemof.standalone` \n5. Now, you can save/restore oemof results to/from DB using:\n```python\n# Example with some hooks\nfrom django_oemof import simulation, hooks\n\nOEMOF_DATAPACKAGE = \"dispatch\"\n\n# Hook functions must be defined beforehand\nph = hooks.Hook(OEMOF_DATAPACKAGE, test_parameter_hook)\nesh = hooks.Hook(OEMOF_DATAPACKAGE, test_es_hook)\nmh = hooks.Hook(OEMOF_DATAPACKAGE, test_model_hook)\n\nhooks.register_hook(hook_type=hooks.HookType.PARAMETER, hook=ph)\nhooks.register_hook(hook_type=hooks.HookType.ENERGYSYSTEM, hook=esh)\nhooks.register_hook(hook_type=hooks.HookType.MODEL, hook=mh)\n\nparameters = {}\nsimulation_id = simulation.simulate_scenario(scenario=OEMOF_DATAPACKAGE, parameters=parameters)\nprint(\"Simulation ID:\", simulation_id)\n\n# Restore oemof results from DB\nfrom django_oemof import models\nsim = models.Simulation.objects.get(id=1)\ninputs, outputs = sim.dataset.restore_results()\n```\n   \n*Note*: `django_oemof.models` must be loaded *AFTER* `init_django()` call. \nThus, import of `django.models` might look unusual and linter might complain - \nbut otherwise django models are not ready yet and a django error will occur! \n\n",
    "bugtrack_url": null,
    "license": "AGPL-3.0",
    "summary": "Django application to run oemof",
    "version": "0.19.0",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d4ecf2e10e8e8d153f7f97268213a53d8688472d60ef9ebc94f1e5286710d34e",
                "md5": "be7abb1e8778a025bc1f823ba1b64889",
                "sha256": "d7573fd2553e1e067855cdf337fa59bd9d99f534eb124eaae82b931c9cd2e7aa"
            },
            "downloads": -1,
            "filename": "django_oemof-0.19.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "be7abb1e8778a025bc1f823ba1b64889",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.9",
            "size": 467811,
            "upload_time": "2025-01-30T11:00:31",
            "upload_time_iso_8601": "2025-01-30T11:00:31.864087Z",
            "url": "https://files.pythonhosted.org/packages/d4/ec/f2e10e8e8d153f7f97268213a53d8688472d60ef9ebc94f1e5286710d34e/django_oemof-0.19.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a4a9da31d5ef02f54b508d36126dd2ef6cac4ba68f38640ecad3897622ffcc98",
                "md5": "9242039ec7a6d01636fe184a36d8eef5",
                "sha256": "dcd3477885a1e5dc430023cbc82a71d5ec726a931766ea66ad4c6b9019992bbc"
            },
            "downloads": -1,
            "filename": "django_oemof-0.19.0.tar.gz",
            "has_sig": false,
            "md5_digest": "9242039ec7a6d01636fe184a36d8eef5",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.9",
            "size": 419657,
            "upload_time": "2025-01-30T11:00:33",
            "upload_time_iso_8601": "2025-01-30T11:00:33.899613Z",
            "url": "https://files.pythonhosted.org/packages/a4/a9/da31d5ef02f54b508d36126dd2ef6cac4ba68f38640ecad3897622ffcc98/django_oemof-0.19.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-01-30 11:00:33",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "django-oemof"
}
        
Elapsed time: 2.14812s