[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python plugin jinja template #4858

Merged
merged 37 commits into from
Feb 16, 2024
Merged

Python plugin jinja template #4858

merged 37 commits into from
Feb 16, 2024

Conversation

joseph-robertson
Copy link
Collaborator
@joseph-robertson joseph-robertson commented Apr 7, 2023

Pull request overview

Addresses Approach 1(A), but using a python measure: #4634 (comment)

I've created two measures:

  • PythonPluginRuby (uses python_plugin_program_erb.py template)
  • PythonPluginPython (uses python_plugin_program_jinja.py template)

Both measures are called by compact_python_plugin.osw.

$ /c/openstudio-3.6.0-alpha/bin/openstudio.exe labs run -w resources/Examples/compact_osw/compact_python_plugin.osw

Appears to be working successfully.

Update: I've removed sys.path.append and tried running with OS v3.7.0 (w/out labs), and it appears to be working successfully.

Pull Request Author

  • Model API Changes / Additions
  • Any new or modified fields have been implemented in the EnergyPlus ForwardTranslator (and ReverseTranslator as appropriate)
  • Model API methods are tested (in src/model/test)
  • EnergyPlus ForwardTranslator Tests (in src/energyplus/Test)
  • If a new object or method, added a test in NREL/OpenStudio-resources: Add Link
  • If needed, added VersionTranslation rules for the objects (src/osversion/VersionTranslator.cpp)
  • Verified that C# bindings built fine on Windows, partial classes used as needed, etc.
  • All new and existing tests passes
  • If methods have been deprecated, update rest of code to use the new methods

Labels:

  • If change to an IDD file, add the label IDDChange
  • If breaking existing API, add the label APIChange
  • If deemed ready, add label Pull Request - Ready for CI so that CI builds your PR

Review Checklist

This will not be exhaustively relevant to every PR.

  • Perform a Code Review on GitHub
  • Code Style, strip trailing whitespace, etc.
  • All related changes have been implemented: model changes, model tests, FT changes, FT tests, VersionTranslation, OS App
  • Labeling is ok
  • If defect, verify by running develop branch and reproducing defect, then running PR and reproducing fix
  • If feature, test running new feature, try creative ways to break it
  • CI status: all green or justified

@joseph-robertson joseph-robertson self-assigned this Apr 7, 2023
from pyenergyplus.plugin import EnergyPlusPlugin

import os, sys
sys.path.append('C:/Python38/Lib/site-packages') # this should (needs to?) be same version as E+'s version
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're going to have to make this path to system python's site_packages some kind of cmake replacement or a command line arg if we want to test this.

But more broadly, this is going to pose the question of whether we should/need to provide some common python packages out of the box like we do for ruby gems (and take into account the pain that it is to maintain that stuff). I'm specifically thinking about native packages (related to this issue #4846), (pandas does have a C extension as well, I think something about numpy may be special)

@kbenne @tijcolem

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @tijcolem, do you have any guidance here? Do we want to make running this OSW into an actual test? Or should these changes (i.e., demonstrating using Python Plugin EMS through both Ruby and Python measures) just be examples that we could point users to?

@jmarrec

Copy link
Collaborator
@jmarrec jmarrec Jul 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following #4868 you can make your test pass the --python_home or --python_path and it'll work (provided the machine it runs on does have jinja2 installed)

I think we should discuss whether we should always add some common python packages like Jinja2 (#4906 (comment))

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jmarrec @kbenne

Has there been any more discussion/movement on #4906?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming the jinja2 package doesn't get bundled into the CLI, what paths would be set for --python_home and --python_path?

@jmarrec

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See here:

if(NOT EXISTS "${Python_ROOT_DIR}")
# Python_STDLIB: we expect it to be
# Unix: ~/.pyenv/versions/3.8.12/lib/python3.8 or
# or on CI: /usr/lib/python3.8/ ... with numpy if install via pip3 and not apt install python3-numpy in `/usr/local/lib/python3.8/dist-packages/`
# Windows C:\Python38\Lib
cmake_path(GET Python_STDLIB PARENT_PATH Python_ROOT_DIR)
if(UNIX)
cmake_path(GET Python_ROOT_DIR PARENT_PATH Python_ROOT_DIR)
endif()
endif()
if(UNIX)
if(EXISTS "${Python_SITELIB}")
set(PYTHON_PATH "${Python_SITELIB}" "${Python_STDLIB}/lib-dynload")
else()
set(PYTHON_PATH "${Python_STDLIB}/lib-dynload")
endif()
if(NOT APPLE)
set(EXTRA_LOCAL_DIST "/usr/local/lib/python3.8/dist-packages")
if (EXISTS "${EXTRA_LOCAL_DIST}")
list(APPEND PYTHON_PATH "${EXTRA_LOCAL_DIST}")
endif()
endif()
else()
set(PYTHON_PATH "$<SHELL_PATH:${Python_SITELIB}>")
endif()
message(DEBUG "PYTHON_PATH=${PYTHON_PATH}")
add_test(NAME OpenStudioCLI.Labs.execute_python_script.numpy.explicit_sys_path_insert
COMMAND $<TARGET_FILE:openstudio> labs execute_python_script execute_python_script_with_numpy.py ${Python_STDLIB}
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/test/"
)
add_test(NAME OpenStudioCLI.Labs.execute_python_script.numpy.python_path
COMMAND $<TARGET_FILE:openstudio> labs
"$<$<BOOL:${PYTHON_PATH}>:--python_path;$<JOIN:${PYTHON_PATH},;--python_path;>>"
execute_python_script execute_python_script_with_numpy.py
COMMAND_EXPAND_LISTS
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/test/"
)
add_test(NAME OpenStudioCLI.Labs.execute_python_script.numpy.python_home
COMMAND $<TARGET_FILE:openstudio> labs
--python_home "$<SHELL_PATH:${Python_ROOT_DIR}>"
execute_python_script execute_python_script_with_numpy.py
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/test/"
)
else()
message(AUTHOR_WARNING "Cannot run the python numpy test, as numpy isn't installed on your system python")

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(py39)julien@OS-build-release$ Products/openstudio labs -c "import jinja2"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'jinja2'
terminate called after throwing an instance of 'std::runtime_error'
  what():  Error executing Python code
Aborted (core dumped)

(py39)julien@OS-build-release$ Products/openstudio labs --python_path /home/julien/.pyenv/versions/3.8.13/lib/python3.8/site-packages -c "import jinja2"

@joseph-robertson joseph-robertson added the Pull Request - Ready for CI This pull request if finalized and is ready for continuous integration verification prior to merge. label Apr 25, 2023
@joseph-robertson joseph-robertson linked an issue Apr 25, 2023 that may be closed by this pull request
2 tasks
@joseph-robertson
Copy link
Collaborator Author

@jmarrec This appears to be working now (i.e., can import pandas and jinja2). How can we formalize running compact_python_plugin.osw as a test? Add something to src/cli/CMakeLists?

@joseph-robertson joseph-robertson marked this pull request as ready for review February 15, 2024 18:23
@@ -0,0 +1,132 @@
import os, sys
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import os, sys
import os
import sys

Generally speaking you should pip install black isort on your system and reformat your python files via

isort .
black -l 120 .

Copy link
Collaborator
@jmarrec jmarrec Feb 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to resources/workflow.

Or it'll get installed: resources/Examples is installed to disk:

install(DIRECTORY "Examples" DESTINATION . COMPONENT "CLI")

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be inside the measures directory inside resources/ , so it is a proper measure and can be moved around.

@jmarrec
Copy link
Collaborator
jmarrec commented Feb 16, 2024

@jmarrec This appears to be working now (i.e., can import pandas and jinja2). How can we formalize running compact_python_plugin.osw as a test? Add something to src/cli/CMakeLists?

Yes. like you've done. But it bothers me we don't check at all if E+ actually runs the python plugins

@jmarrec
Copy link
Collaborator
jmarrec commented Feb 16, 2024

@joseph-robertson I've made some adjustments (moved things around, linted) + actually tested the plugins are running.

Assuming CI comes clean and you're ok with the changes, this can drop.

@joseph-robertson joseph-robertson merged commit 02cf3b8 into develop Feb 16, 2024
4 of 6 checks passed
@joseph-robertson joseph-robertson deleted the plugin-jinja-template branch February 16, 2024 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Pull Request - Ready for CI This pull request if finalized and is ready for continuous integration verification prior to merge.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Python plugin next steps
3 participants