Message Boards Message Boards

Packages and Notebooks as a Databin

Posted 9 years ago

Can I use Databins to share packages and notebooks?

If yes. How?

bin = CreateDatabin[]
DatabinAdd[bin, <|"myPack" -> "???"|>]

How can I load a Package stored in a Databin?

Same question in SE: here

POSTED BY: Rodrigo Murta
4 Replies

Tks Pieter.

I thought that it would make more elegant to exchange Wolfram code in this way. So users could interact directly with the repository, without need to use DropBox or it own server, and all could be done directly in Mathematica.

If I could just do something like:

DatabinAdd[mybin,"FilePath/myPackName.wl"]

and then, to load it, do:

Get@mybin["myPackName`"]

or

Needs@mybin["myPackName`"]

Would be cool!

But I believe that it's not the main pourpose of Databin (now). And that neither Get or Needs were overload to do this.

Pity. It would be nice for quick code exchange in forums and in the community.

POSTED BY: Rodrigo Murta
Posted 9 years ago

Here is an overloading sugestion:

Unprotect@File;
File/:DatabinAdd[mybin_Databin, File[packPath_]]:=Module[{},
    DatabinAdd[mybin, <|FileBaseName@packPath<>"`"->Import[packPath,"Text"]|>]
]
Protect@File;

So, to add you package into a Databin, just do:

DatabinAdd[Databin["3GUtgBVI"], File["/Desktop/dropTest.wl"]]

Now, to load it, let's work with Get:

Unprotect@Get;
Get[pack_String,Databin[id_String]]:=Module[{databin=Databin[id]},
    Get[pack,databin]
]

Get[pack_String,databin_Databin]:=Module[{packContent,path},
    packContent=Lookup[databin["Values"],pack,Return[$Failed]][[1]];
    path=FileNameJoin@{$TemporaryDirectory,StringReplace[pack,"`"-> ""]<>".wl"};
    Export[path, packContent, "Text"];
    Get[Evaluate@path]
]
Protect@Get;

Now, to load you package, just do:

Get["dropTest`", Databin["3GUtgBVI"]]

now:

myFunc[1]
2
POSTED BY: Updating Name

Some problem with my post. I can't edit it, and my name is missing.

This code part is useless:

Get[pack_String,Databin[id_String]]:=Module[{databin=Databin[id]},
    Get[pack,databin]
]

Can be removed

Murta

POSTED BY: Rodrigo Murta
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract