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

feat: create new environment using a remote mamba solver #141

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions mamba_gator/envmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,15 +395,19 @@ async def import_env(
Returns:
Dict[str, str]: Create command output
"""
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=file_name) as f:
regex = re.compile('^@EXPLICIT$', re.MULTILINE)
is_explicit_list = regex.search(file_content)

env_argument = [] if is_explicit_list else ["env"]

with tempfile.NamedTemporaryFile(mode="w", suffix=file_name) as f:
name = f.name
f.write(file_content)
f.flush()

ans = await self._execute(
self.manager, "env", "create", "-q", "--json", "-n", env, "--file", name
)
# Remove temporary file
os.unlink(name)
ans = await self._execute(
self.manager, *env_argument, "create", "-q", "--json", "-n", env, "--file", name
)

rcode, output = ans
if rcode > 0:
Expand Down Expand Up @@ -995,3 +999,13 @@ async def remove_packages(self, env: str, packages: List[str]) -> Dict[str, str]
)
_, output = ans
return self._clean_conda_json(output)

async def get_subdir(self):
rcode, output = await self._execute(
self.manager, "list", "--explicit"
)
if rcode == 0:
regex = re.compile('^# platform: ([a-z\-0-9]+)$', re.MULTILINE)
return {'subdir': regex.findall(output)[0]}

raise RuntimeError('subdir not found')
10 changes: 10 additions & 0 deletions mamba_gator/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,15 @@ def delete(self, index: int):
self.finish()


class SubdirHandler(EnvBaseHandler):
@tornado.web.authenticated
async def get(self):
"""`GET /subdir` Get the conda-subdir.
"""
idx = self._stack.put(self.env_manager.get_subdir)

self.redirect_to_task(idx)

# -----------------------------------------------------------------------------
# URL to handler mappings
# -----------------------------------------------------------------------------
Expand All @@ -488,6 +497,7 @@ def delete(self, index: int):
(r"/channels", ChannelsHandler),
(r"/environments", EnvironmentsHandler), # GET / POST
(r"/environments/%s" % _env_regex, EnvironmentHandler), # GET / PATCH / DELETE
(r"/subdir", SubdirHandler), # GET
# PATCH / POST / DELETE
(r"/environments/%s/packages" % _env_regex, PackagesEnvironmentHandler),
(r"/packages", PackagesHandler), # GET
Expand Down
68 changes: 67 additions & 1 deletion mamba_gator/navigator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
ExtensionHandlerMixin,
)
from jupyter_server.utils import url_path_join as ujoin
from jupyter_core.application import base_aliases
from jupyterlab_server import LabServerApp
from traitlets import Unicode, Dict, Bool
from mamba_gator._version import __version__
from mamba_gator.handlers import _load_jupyter_server_extension
from mamba_gator.log import get_logger
Expand All @@ -20,13 +22,20 @@
class MambaNavigatorHandler(
ExtensionHandlerJinjaMixin, ExtensionHandlerMixin, JupyterHandler
):
extra_settings = None

def initialize(self, extra_settings, **kwargs):
self.extra_settings = extra_settings
super().initialize(**kwargs)

def get(self):
config_data = {
"appVersion": __version__,
"baseUrl": self.base_url,
"token": self.settings["token"],
"fullStaticUrl": ujoin(self.base_url, "static", self.name),
"frontendUrl": ujoin(self.base_url, "gator/"),
**self.extra_settings
}
return self.write(
self.render_template(
Expand Down Expand Up @@ -56,8 +65,65 @@ class MambaNavigator(LabServerApp):
user_settings_dir = os.path.join(HERE, "user_settings")
workspaces_dir = os.path.join(HERE, "workspaces")

quetz_url = Unicode(
'',
config=True,
help="The Quetz server to use for creating new environments"
)

quetz_solver_url = Unicode(
'',
config=True,
help="The Quetz server to use for solving, if this is a different server than 'quetzUrl'",
)

companions = Dict(
{},
config=True,
help="{'package name': 'semver specification'} - pre and post releases not supported",
)

from_history = Bool(
False,
config=True,
help="Use --from-history or not for `conda env export`",
)

types = Dict(
{
"Python 3": ["python=3", "ipykernel"],
"R": ["r-base", "r-essentials"]
},
config=True,
help="Type of environment available when creating it from scratch.",
)

white_list = Bool(
False,
config=True,
help="Show only environment corresponding to whitelisted kernels",
)

aliases = dict(base_aliases)
aliases.update({
'quetz_url': 'MambaNavigator.quetz_url',
'quetz_solver_url': 'MambaNavigator.quetz_solver_url',
'companions': 'MambaNavigator.companions',
'from_history': 'MambaNavigator.from_history',
'types': 'MambaNavigator.types',
'white_list': 'MambaNavigator.white_list'
})

def initialize_handlers(self):
self.handlers.append(("/gator", MambaNavigatorHandler))
self.handlers.append(("/gator", MambaNavigatorHandler, dict(
extra_settings=dict(
quetzUrl=self.quetz_url,
quetzSolverUrl=self.quetz_solver_url,
companions=self.companions,
fromHistory=self.from_history,
types=self.types,
whiteList=self.white_list
))))
super().initialize_handlers()

def start(self):
Expand Down
3 changes: 3 additions & 0 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
"@lumino/coreutils": "^1.5.3",
"@lumino/signaling": "^1.4.3",
"@lumino/widgets": "^1.16.1",
"codemirror": "^5.60.0",
"codemirror-show-hint": "^5.58.3",
"jupyterlab_toastify": "^4.1.3",
"d3": "^5.5.0",
"react-d3-graph": "^2.5.0",
Expand All @@ -60,6 +62,7 @@
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@jupyterlab/testutils": "^3.0.0",
"@types/codemirror": "^0.0.108",
"@types/jest": "^26.0.0",
"@types/react": "^17.0.0",
"@types/react-d3-graph": "^2.3.4",
Expand Down
5 changes: 5 additions & 0 deletions packages/common/src/components/CondaEnvList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export interface IEnvListProps {
* Environment remove handler
*/
onRemove(): void;
/**
* Environment solve handler
*/
onSolve?: (() => void) | false;
}

/**
Expand Down Expand Up @@ -92,6 +96,7 @@ export const CondaEnvList: React.FunctionComponent<IEnvListProps> = (
onExport={props.onExport}
onRefresh={props.onRefresh}
onRemove={props.onRemove}
onSolve={props.onSolve}
/>
<div
id={CONDA_ENVIRONMENT_PANEL_ID}
Expand Down
192 changes: 192 additions & 0 deletions packages/common/src/components/CondaEnvSolve.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
import * as React from 'react';
import CodeMirror, { Editor } from 'codemirror';
import 'codemirror/lib/codemirror.css';
import './yaml';
import * as condaHint from './CondaHint';
import { IEnvironmentManager } from '../tokens';
import { INotification } from 'jupyterlab_toastify';

/**
* Conda solve properties
*/
export interface ICondaEnvSolveProps {
/**
* The URL of the Quetz server
*/
quetzUrl: string;
/**
* The URL of the Quetz server with the solver plugin, if different from quetzUrl
*/
quetzSolverUrl: string;
/**
* The Conda subdir (or platform e.g. osx-64, linux-32) of the backend
*/
subdir: string;
/**
* The initial content of the editors text area
*/
content?: string;
/**
* Should the channel name be expanded to the channel URL in the editors text area
*/
expandChannelUrl?: boolean;
/**
* Callback to get notified of changes to the editors text area
*/
onContentChange?(content: string): void;
}

export function CondaEnvSolve(props: ICondaEnvSolveProps): JSX.Element {
condaHint.register(props.quetzUrl, props.expandChannelUrl);
const codemirrorElem = React.useRef(null);

const [editor, setEditor] = React.useState(null);

React.useEffect(() => {
if (editor) {
if (props.content !== undefined && props.content !== editor.getValue()) {
editor.setValue(props.content);
editor.refresh();
}
return;
}
const newEditor = CodeMirror(codemirrorElem.current, {
value: props.content || '',
lineNumbers: true,
extraKeys: {
'Ctrl-Space': 'autocomplete',
'Ctrl-Tab': 'autocomplete'
},
tabSize: 2,
mode: 'yaml',
autofocus: true
});
if (props.onContentChange) {
newEditor.on('change', (instance: Editor) =>
props.onContentChange(instance.getValue())
);
}
setEditor(newEditor);

/* Apply lab styles to this codemirror instance */
codemirrorElem.current.childNodes[0].classList.add('cm-s-jupyter');
});
return (
<div
ref={codemirrorElem}
className="conda-complete-panel"
onMouseEnter={() => editor && editor.refresh()}
/>
);
}

/**
* Solve the environment provided in environment_yml on the Quetz server and create it on the
* backend.
*
* @param environment_yml - The environment.yml content
* @param environmentManager - The Conda environment manager
* @param expandChannelUrl - The environment_yml contains channel names that should be expanded to
* channel URLS
* @param onMessage - Callback to provide feedback about the process
*/
export async function solveAndCreateEnvironment(
environment_yml: string,
environmentManager: IEnvironmentManager,
expandChannelUrl: boolean,
onMessage?: (msg: string) => void
): Promise<void> {
mariobuikhuizen marked this conversation as resolved.
Show resolved Hide resolved
const name = condaHint.getName(environment_yml);
const { quetzUrl, quetzSolverUrl, subdir } = environmentManager;

let message = 'Solving environment...';
onMessage && onMessage(message);
let toastId = await INotification.inProgress(message);
try {
const explicitList = await condaHint.fetchSolve(
quetzUrl,
quetzSolverUrl,
(await subdir()).subdir,
environment_yml,
expandChannelUrl
);
await INotification.update({
toastId,
message: 'Environment has been solved.',
type: 'success',
autoClose: 5000
});

message = `creating environment ${name}...`;
onMessage && onMessage(message);
toastId = await INotification.inProgress(message);
await environmentManager.import(name, explicitList);

message = `Environment ${name} created.`;
onMessage && onMessage(message);
await INotification.update({
toastId,
message,
type: 'success',
autoClose: 5000
});
} catch (error) {
onMessage && onMessage(error.message);
if (toastId) {
await INotification.update({
toastId,
message: error.message,
type: 'error',
autoClose: 0
});
}
}
}

export interface ICondaEnvSolveDialogProps {
/**
* The Conda subdir (or platform e.g. osx-64, linux-32) of the backend
*/
subdir: string;
/**
* The Conda environment manager
*/
environmentManager: IEnvironmentManager;
}

export function CondaEnvSolveDialog(
props: ICondaEnvSolveDialogProps
): JSX.Element {
const [environment_yml, setEnvironment_yml] = React.useState('');
const [solveState, setSolveState] = React.useState(null);

return (
<div className="condaCompleteDialog__panel">
<div style={{ flexGrow: 1 }}>
<CondaEnvSolve
expandChannelUrl={false}
subdir={props.subdir}
quetzUrl={props.environmentManager.quetzUrl}
quetzSolverUrl={props.environmentManager.quetzSolverUrl}
onContentChange={setEnvironment_yml}
/>
</div>
<div style={{ padding: '12px' }}>
<button
onClick={() =>
solveAndCreateEnvironment(
environment_yml,
props.environmentManager,
true,
setSolveState
)
}
className="jp-Dialog-button jp-mod-accept jp-mod-styled"
>
Create
</button>
<span style={{ marginLeft: '12px' }}>{solveState}</span>
</div>
</div>
);
}
Loading