Skip to content

Commit

Permalink
Optimize preference ui
Browse files Browse the repository at this point in the history
  • Loading branch information
iplai committed Feb 17, 2024
1 parent 355df8a commit 312dccb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
4 changes: 3 additions & 1 deletion marimo_blender/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"author": "iplai",
"description": "Reactive notebook for Python integrated in blender",
"blender": (2, 80, 0),
"version": (0, 2, 0),
"version": (0, 2, 1),
"location": "View 3D > Header Menu > Notebook",
"doc_url": "https://github.com/iplai/marimo-blender",
"tracker_url": "https://github.com/iplai/marimo-blender/issues",
Expand All @@ -16,6 +16,7 @@
from .preferences import (
MarimoAddonPreferences,
InstallPythonModules,
InstallPythonModule,
UninstallPythonModules,
ListPythonModules,
StartMarimoServer,
Expand All @@ -30,6 +31,7 @@ def marimo_header_btn(self: bpy.types.Menu, context):
classes = (
MarimoAddonPreferences,
InstallPythonModules,
InstallPythonModule,
UninstallPythonModules,
ListPythonModules,
StartMarimoServer,
Expand Down
10 changes: 10 additions & 0 deletions marimo_blender/addon_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,16 @@ def replace_marimo_module():
)
)

def install_python_module(self, module_name, line_callback=None, finally_callback=None):
self.exec_command(
sys.executable, '-m', 'pip', 'install',
'--disable-pip-version-check',
'--no-input',
'--exists-action', 'i',
module_name,
line_callback=line_callback,
finally_callback=finally_callback)

def uninstall_python_modules(self, line_callback=None, finally_callback=None):
self.exec_command(
sys.executable, '-m', 'pip', 'uninstall',
Expand Down
52 changes: 43 additions & 9 deletions marimo_blender/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@ def execute(self, context):
return {'FINISHED'}


class InstallPythonModule(bpy.types.Operator):
"""Install Python Module """
bl_idname = 'marimo.install_python_module'
bl_label = 'Install Python Module'
bl_options = {'REGISTER', 'INTERNAL'}

module_name: bpy.props.StringProperty(name="Module Name", default="")

@classmethod
def poll(cls, context):
return not addon_setup.installer.is_running

def execute(self, context):
_LINES.clear()
region = context.region
addon_setup.installer.install_python_module(
self.module_name,
line_callback=lambda line: _lines_append(line) or region.tag_redraw(),
finally_callback=lambda e: region.tag_redraw(),
)
return {'FINISHED'}


class UninstallPythonModules(bpy.types.Operator):
"""Uninstall Python Module marimo dependencies"""
bl_idname = 'marimo.uninstall_python_modules'
Expand Down Expand Up @@ -120,10 +143,11 @@ class MarimoAddonPreferences(bpy.types.AddonPreferences):
bl_idname = __package__

port: bpy.props.IntProperty(
name="Port of Marimo Server",
name="Server Port",
default=2718,
)
show_logs: bpy.props.BoolProperty(default=False)
module_name: bpy.props.StringProperty(name="Module Name", default="")

def draw(self, context: bpy.types.Context):
layout = self.layout
Expand All @@ -133,16 +157,26 @@ def draw(self, context: bpy.types.Context):
row.operator(StartMarimoServer.bl_idname, icon='URL')
# row.operator(StopMarimoServer.bl_idname, icon='X')

col = layout.column(align=True)
flow = col.grid_flow(align=True)
flow.row().label(text="Required Python Modules:")
row = layout.row()
row.label(text="Required Python Modules:")
row.operator(InstallPythonModules.bl_idname, icon="PREFERENCES")

row = layout.row(align=True)
flow = row.grid_flow(align=True)

for name, is_installed in addon_setup.installer.get_required_modules().items():
flow.row().label(text=name, icon='CHECKMARK' if is_installed else 'QUESTION' if name == 'fake-bpy-module' else 'ERROR')
flow = col.grid_flow(align=True)
flow.row().label(text=name, icon='CHECKMARK' if is_installed else 'REC' if name == 'fake-bpy-module' else 'ERROR')

row = layout.row()
row.operator(UninstallPythonModules.bl_idname)
row.operator(ListPythonModules.bl_idname)

row = layout.row()
flow = row.grid_flow(align=True)
row = flow.row(align=True)
row.prop(self, 'module_name')
row = flow.row(align=True)
row.operator(InstallPythonModules.bl_idname)
flow.row().operator(UninstallPythonModules.bl_idname)
flow.row().operator(ListPythonModules.bl_idname)
row.operator(InstallPythonModule.bl_idname, icon='PLUS').module_name = self.module_name

col = layout.column(align=False)
row = col.row(align=True)
Expand Down

0 comments on commit 312dccb

Please sign in to comment.