9
$\begingroup$

Mathematica Stack Exchange accepts HTML in the questions, and therefore text such as

<undefined rubbish>

should be ignored when formatting the question for display, but still be available when editing the question.

How can we take advantage of this to elegantly store data in the question that can be easily retrieved using minimal Mathematica code?

A plain text solution would be ideal.

The storage limits should be worked out.

I'm looking for a technical answer, in the hope that developing that would give more options to answer this other question in meta. A different topic is if doing this would break the rules of Mma SE, of if using this method could be advisable or not. That is to be discussed in meta, here.

$\endgroup$
  • 2
    $\begingroup$ I think this should go on meta instead. You've seen the recent related question there. $\endgroup$ – Szabolcs Apr 25 '17 at 11:41
  • 2
    $\begingroup$ I thought about that, but I want to look for a technical answer that would make the code short and the use seamless. If that becomes a valid policy, should be a meta discussion. $\endgroup$ – rhermans Apr 25 '17 at 11:42
  • 3
    $\begingroup$ While such a feature sounds convenient I'd personally be hesitant to use it because importing and running code directly from a website is dangerous. (still +1 from me) $\endgroup$ – Sascha Apr 25 '17 at 15:12
  • 1
    $\begingroup$ Related: (74198). SE-Tools can also do this (SEEncodeExpression/SEDecodeImage). $\endgroup$ – Michael E2 Apr 25 '17 at 17:27
  • $\begingroup$ To be fair, anybody using the SE Uploader palette is also running Import[url1][url2] that is, executing code from url1. $\endgroup$ – rhermans May 9 '17 at 12:43
7
$\begingroup$

Store data in question

Tables could be left in plain text, with some encoding to remove CR and LF characters and other symbols.

EncodeForQuestion[expr_List] := 
 CopyToClipboard[
  StringJoin["<data payload=\"", 
    URLEncode[ExportString[expr, "CSV"]], "\"/>"]]

Arbitrary data can be expressed as plain text with InputForm and ToString or compressed (Compress) into plain text and wrapped into "<>" markers

InputFormForQuestion[expr_] := 
 CopyToClipboard[
  StringJoin["<data payload=\"", 
    URLEncode[ToString[InputForm[expr]]], "\"/>"]]


CompressForQuestion[expr_] := CopyToClipboard[
  StringJoin[
   "<data payload=\""
   , Compress[expr]
   , "\"/>"]
  ]

and pasted at the end of the question.

Mathematica graphics

This has the advantage that the display of the question is not affected, the format is obvious and can be retrieved by hand if necessary, going to edit the question to then copy&paste the data. Hopefully in the future the browser add-on could provide a link to the data to download.

Programmatically retrieve the data

The plain text markdown version of the question body can be retrieved by using the Stack Exchange API without evaluating, for inspection.

GetQuestionPlain[SEQuestionId_Integer] := Import[
   URLBuild[
    {"https://api.stackexchange.com", "2.2", "posts", ToString[SEQuestionId]}
    , {"site" -> "mathematica", "filter" -> "!SWK9z)LGKmEEgF5)yL"}]
   , "RawJSON"][["items", 1, "body_markdown"]]

The data stored in the question can be downloaded and parsed using

GetDataFromQuestion[SEQuestionId_Integer] := 
 StringCases[GetQuestionPlain[SEQuestionId], 
  "&lt;data payload=&quot;" ~~ payload___ ~~ "&quot;/&gt;" :> payload]

Where GetDataFromQuestion takes as argument the question ID available in the URL.

Example:

Mathematica graphics

Uncompress/@GetDataFromQuestion[144472]

Mathematica graphics

Notice that this allows multiple sets of data.

To do

  • Work out storage limits.
  • Use labels to identify multiple payloads

Feel free to edit this answer.

$\endgroup$
  • $\begingroup$ Trick,and Any convenient method can get that SEQuestionId? $\endgroup$ – yode Apr 25 '17 at 11:37
  • $\begingroup$ Could I give a suggestion for that parameter can be SEQuestionId_Integer|websiteAddress?If it can accept websiteAddress,then we just need right-click that share.And I don't know why it will pop this windows when I run your code.. $\endgroup$ – yode Apr 25 '17 at 11:42
  • $\begingroup$ Does anybody else get that pop-up window? I don't get it even if logged off Wolfram. $\endgroup$ – rhermans Apr 25 '17 at 12:27
  • $\begingroup$ You can get the SEQuestionID by copying the Share URL and then URLParse[NotebookGet[ClipboardNotebook[]][[1, 1, 1]]][["Path", 3]] $\endgroup$ – rhermans Apr 25 '17 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.