"use strict";(self.webpackChunkjlmc=self.webpackChunkjlmc||[]).push([[137],{72137:(e,t,n)=>{n.r(t),n.d(t,{default:()=>Z});var a=n(79311),o=n(58759),s=n(38337);const l="create-model-card",i="modify-model-card-stage",r=new Map([["plotting","Plotting"],["datacleaning","Data Cleaning"],["preprocessing","Preprocessing"],["hyperparameters","Hyperparameters"],["modeltraining","Model Training"],["modelevaluation","Model Evaluation"],["miscellaneous","Ignore"]]);var c=n(49509),d=n(19129),p=n(23706),h=(n(26946),n(66678)),m=n.n(h),u=n(56271),_=n.n(u),g=n(14456),x=n(83396),v=n(73426),C=n(7179),b=n(70938),w=n.n(b),k=n(40654),E=n.n(k),f=n(99946),y=n(19850);const M=/<!--\s*@md-(\w*)\s*-->/,P=/<!--\s*@md-\w*\s*-->([\s\S]*)<!--\s*\/md-\w*\s*-->/;var R=n(90608);const S=(e,t)=>{setTimeout((()=>{e.deselectAll(),e.activeCellIndex=t,function(e,t=!1){const n=e.activeCell;"edit"===e.mode&&n&&(n.editor.hasFocus()||n.editor.focus()),t&&!e.node.contains(document.activeElement)&&e.node.focus()}(e),e.mode="edit",function(e,t){R.ElementExt.scrollIntoViewIfNeeded(e.node,t.node),e.deselectAll(),e.select(t),t.activate()}(e,e.activeCell)}),0)};var j=n(61209),N=n(77045);const $=({sectionName:e,sectionTitle:t,annotMap:n,updateAnnotMap:a,notebook:o,idx:s})=>n.has(e)?_().createElement(j.Z,{onClick:()=>{S(o,n.get(e).idx)}}):_().createElement(v.Popconfirm,{title:`Add a ${r.has(e)?"description":"new cell"} for ${t}?`,onConfirm:()=>{var n,l;o.model.cells.insert(s,new N.MarkdownCellModel({cell:{cell_type:"markdown",source:(n=e,l=t,`# ${l}\n\x3c!-- @md-${n} --\x3e\n\x3c!-- /md-${n} --\x3e`),metadata:{}}})),a((t=>{t.set(e,s)})),S(o,s)},okText:"Yes",cancelText:"No"},_().createElement(j.Z,null));(0,C.enableMapSet)();const I=(e,t)=>"author"===e?1:r.has(e)&&t.cell_ids.length>0?t.cell_ids[0]:0,W=E().div`
  position: relative;
  background: aliceblue;
  width: 40%;
  height: 40px;
  border-radius: 10px;
`,D=E().div`
  position: absolute;
  left: ${e=>e.left}%;
  height: 100%;
  width: 5px;
  background-color: lightskyblue;
  border-radius: 15px;
  transition: transform 0.2s, background-color 0.2s;

  &:hover {
    transform: scale(3, 1.5);
    background-color: #1890ff;
    z-index: 2;
    cursor: pointer;
  }
`,T=({notebook:e,sectionName:t,sectionContent:n,quickFix:a})=>"object"!=typeof n?null:_().createElement(_().Fragment,null,_().createElement(_().Fragment,null,_().createElement("h1",null,n.title," ",a),_().createElement(w(),null,n.description),_().createElement("div",{style:{display:"block"}},"modelname"!==t&&"cell_ids"in n&&n.cell_ids.length>0?_().createElement(W,null,n.cell_ids.map(((t,n)=>_().createElement(D,{key:n,left:t/e.model.cells.length*100,onClick:()=>S(e,t-1)})))):null),"figures"in n?n.figures.map(((e,t)=>_().createElement("img",{style:{display:"block"},key:t,src:`data:image/png;base64,${e}`}))):null)),B=({notebook:e,context:t,docManager:n,ServerResponse:a,handler:o})=>{const[s,l]=(0,f.useImmer)(new Map),[i,r]=(0,f.useImmer)({});return(0,u.useEffect)((()=>{const t=(e=>{const t=new Map;if(e.model.cells){let n,a;return(0,y.each)(e.model.cells,((e,o)=>{"markdown"===e.type&&(n=e.value.text.match(M),a=e.value.text.match(P),n&&t.set(n[1],{idx:o,content:a[1]}))})),t}})(e);let n=JSON.parse(JSON.stringify(a));t.forEach(((e,t)=>{t in n&&(n[t].description=e.content)})),r((()=>n));const o="modelname";if(void 0!==n.modelname.description&&!t.has(o)){const a=e.model.cells.get(0).value;a.insert(0,"\x3c!-- @md-modelname --\x3e\n"),a.insert(a.text.length,"\n\x3c!-- /md-modelname --\x3e"),t.set(o,{idx:0,content:n.modelname.description})}l((()=>t))}),[e]),_().createElement(_().Fragment,null,_().createElement(v.Row,{style:{position:"sticky",top:10,float:"right"}},_().createElement(v.Space,null,_().createElement(v.Col,{span:6},_().createElement(v.Button,{type:"primary",onClick:()=>{o()}},"Refresh")),_().createElement(v.Col,{span:6},_().createElement(v.Button,{type:"primary",onClick:()=>{const e=x.PathExt.dirname(t.path);let a=x.PathExt.basename(t.path);a=a.split(x.PathExt.extname(a))[0],a=a.split(" ").join("_"),a="card_"+a+".md";const o=x.PathExt.join(e,a);let s=n.findWidget(o,"default");void 0===s&&(s=n.createNew(o)),n.openOrReveal(s.context.path),s.context.ready.then((()=>{s.content.model.value.text=(e=>{let t="";return Object.entries(e).forEach((([e,n])=>{if("miscellaneous"===e)return;if("modelname"===e)return void(t+=`${n.description}\n`);const a=n.description&&"string"==typeof n.description&&n.description.trim(),o=n.figures&&n.figures.length;(a||o)&&(t+=`## ${n.title}\n`),a&&(t+=`${n.description}\n`),o&&n.figures.forEach(((e,n)=>t+=`<img alt="figure${n}" src="data:image/png;base64, ${e}">\n`))})),t})(i)}))}},"Export to MD")))),Object.entries(i).map((([t,n],a)=>"miscellaneous"===t?null:_().createElement(T,{key:a,notebook:e,sectionName:t,sectionContent:n,quickFix:_().createElement($,{sectionName:t,sectionTitle:n.title,annotMap:s,updateAnnotMap:l,notebook:e,idx:I(t,n)})}))))};class A extends o.ReactWidget{constructor(e,t,n,a){super(),this._notebook=e.content,this._context=e.context,this._docManager=t,this.serverResponse=n,this.createPanelHandler=a,this.addClass("jp-ReactWidget")}onUpdateRequest(){g.render(_().createElement(B,{notebook:this._notebook,context:this._context,docManager:this._docManager,ServerResponse:this.serverResponse,handler:this.createPanelHandler}),this.node)}updateModel(e){this._notebook=m()(e.content)}render(){return _().createElement(B,{notebook:this._notebook,context:this._context,docManager:this._docManager,ServerResponse:this.serverResponse,handler:this.createPanelHandler})}}var O=n(57254);const F=/(\[model card\] stage: )[\w ]*(.*)/,H=({notebook:e})=>{const t=_().createElement(v.Menu,null,Array.from(r.entries()).map((([t,n],a)=>_().createElement(v.Menu.Item,{key:a,onClick:()=>{e.activeCell.model.metadata.set("stage",t);const a=e.activeCell.model.value.text;a.match(F)?e.activeCell.model.value.text=a.replace(F,`$1${n}$2`):e.activeCell.model.value.insert(0,`# [model card] stage: ${n}\n`)}},n))));return _().createElement(v.Dropdown,{overlay:t},_().createElement(v.Button,null,"Select stage ",_().createElement(O.Z,null)))};class V extends o.ReactWidget{constructor(e){super(),this._notebook=e.content}updateModel(e){this._notebook=m()(e.content)}render(){return _().createElement(H,{notebook:this._notebook})}}var q=n(78251);class J extends p.StackedPanel{constructor(e,t,n,a,o){super(),this._app=e,this._docManager=t,this.id=n,this.title.label=a,this.title.closable=!0,this._createPanelHandler=o}onUpdateRequest(){this._view?this.updateView(this._context.localPath):this.createView(this._context.localPath)}setContext(e){this._context=e}setPanel(e){this._panel=e}getPopup(){return this._popup}async getData(e){const t={path:e};try{return await async function(e="",t={}){const n=q.ServerConnection.makeSettings(),a=x.URLExt.join(n.baseUrl,"jlmc",e);let o;try{o=await q.ServerConnection.makeRequest(a,t,n)}catch(e){throw new q.ServerConnection.NetworkError(e)}const s=await o.json();if(!o.ok)throw new q.ServerConnection.ResponseError(o,s.message);return s}("hello",{body:JSON.stringify(t),method:"POST"})}catch(e){console.error(`Error on POST /jlmc/hello ${t}.\n${e}`),alert("jlmc ran into errors. Generation Failed.")}}async updateView(e){this.getData(e).then((e=>{this._view=new A(this._panel,this._docManager,e,this._createPanelHandler),this._view.updateModel(this._panel),this._view.update(),this._popup=new V(this._panel),this._popup.updateModel(this._panel)}))}launchPanel(){this._popup&&(this._popup.updateModel(this._panel),new c.Popup({body:this._popup,anchor:this._panel.content.activeCell,align:"right"}).launch())}async createView(e){this.getData(e).then((e=>{this._view=new A(this._panel,this._docManager,e,this._createPanelHandler),this.addWidget(this._view),this._popup=new V(this._panel),this._view.updateModel(this._panel),this._popup.updateModel(this._panel)}))}createNew(e,t){return new d.DisposableDelegate((()=>{this.widgets&&this.widgets.forEach((e=>{e.isDisposed||e.dispose(),e=null})),this._popup.dispose()}))}}function U(e){for(var t="",n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",a=n.length,o=0;o<e;o++)t+=n.charAt(Math.floor(Math.random()*a));return t}class L{constructor(e,t){this._app=e,this._docManager=t}createNew(e,t){let n;function a(e){e.launchPanel()}const s=()=>{t.ready.then((()=>{this._context=t;let o=x.PathExt.basename(t.path);o=o.split(x.PathExt.extname(o))[0],o=o.split(" ").join("_"),o+=".modelcard",n?n&&!n.isAttached?(n.setContext(this._context),n.setPanel(e),this._app.shell.add(n,"main",{mode:"split-right"}),this._app.shell.activateById(n.id),this._modelCardPanel=n,n.update()):n&&!n.isVisible?(n.setContext(this._context),n.setPanel(e),this._app.shell.activateById(n.id),this._modelCardPanel=n,n.update()):n&&(this._app.shell.currentWidget===n?(this._app.shell.currentWidget.dispose(),n=new J(this._app,this._docManager,U(10),o,s),n.setContext(this._context),n.setPanel(e),this._modelCardPanel=n,this._app.commands.hasCommand(i)||(this._app.commands.addCommand(i,{label:"[Model Card] Change stage to...",execute:()=>{a(this._modelCardPanel)}}),this._app.contextMenu.addItem({command:i,selector:".jp-CodeCell"})),this._app.docRegistry.addWidgetExtension("Notebook",n),this._app.shell.add(n,"main",{mode:"split-right"}),this._app.shell.activateById(n.id),n.update()):(n.setContext(this._context),n.setPanel(e),n.update())):(n=new J(this._app,this._docManager,U(10),o,s),n.setContext(this._context),n.setPanel(e),this._modelCardPanel=n,this._app.commands.hasCommand(i)||(this._app.commands.addCommand(i,{label:"[Model Card] Change stage to...",execute:()=>{a(this._modelCardPanel)}}),this._app.contextMenu.addItem({command:i,selector:".jp-CodeCell"})),this._app.docRegistry.addWidgetExtension("Notebook",n),this._app.shell.add(n,"main",{mode:"split-right"}),this._app.shell.activateById(n.id),n.update())}))},r=new o.ToolbarButton({tooltip:"Generate model card",className:"myButton",onClick:()=>{s()},label:"Model Card"});return e.toolbar.insertItem(0,"jupyterlabClassic",r),this._app.commands.hasCommand(l)||this._app.commands.addCommand(l,{label:"Model Card",caption:"Model Card",isVisible:()=>!1,execute:s}),r}}const Z={id:"model-card-extension",autoStart:!0,requires:[s.IDocumentManager,a.ILayoutRestorer],activate:(e,t,n)=>{console.log("JupyterLab extension jlcards is activated!");const a=new o.WidgetTracker({namespace:"model-card"});n.restore(a,{command:l,name:()=>"model-card"});const s=new L(e,t);e.docRegistry.addWidgetExtension("Notebook",s)}}}}]);