pony-tutorial icon indicating copy to clipboard operation
pony-tutorial copied to clipboard

Add ability to run code examples in the playground

Open SeanTAllen opened this issue 6 years ago • 18 comments

We had this sort of on the old tutorial but that didn't get migrated over.

SeanTAllen avatar Jan 05 '19 17:01 SeanTAllen

I went looking through the mkdocs material for this functionality and didn't see it. I'm going to reach out to the author and see if I missed the functionality, otherwise, I'll put in a feature request.

SeanTAllen avatar Feb 08 '22 01:02 SeanTAllen

So this is the report:

We can do some custom css and javascript. That's not really my thing, but someone could take this on.

https://github.com/squidfunk/mkdocs-material/discussions/3563

SeanTAllen avatar Feb 10 '22 13:02 SeanTAllen

There doesn't seem to be a native option to add any other button than "copy" and "select" with the latter being sponsors-only: grafik

I went looking through the mkdocs material for this functionality and didn't see it. I'm going to reach out to the author and see if I missed the functionality, otherwise, I'll put in a feature request.

Others have also wished for this functionality to become more generalized: https://github.com/squidfunk/mkdocs-material/discussions/4005 The only solution given there is to fork the theme.

However, one could add custom JavaScript to add this behavior: https://squidfunk.github.io/mkdocs-material/customization/#additional-javascript

grafik

To achieve a quick and easy solution, we could add buttons to all code blocks manually

grafik

Something, that would be more work but would prevent us from duplicating the code (once inside the code block and once URL-encoded in the button), would be to move the code to separate files and embed them. Alternatively, it could be gists.

This could also help in cases where the examples are longer than the URL is allowed to be by the browser (I don't know if we would run into that problem) 🤔

Or should the preview be in the tutorial, not just (in a new tab) in the playground?

shaedrich avatar Apr 02 '24 21:04 shaedrich

We use the insiders build. So that is available as an option.

SeanTAllen avatar Apr 03 '24 00:04 SeanTAllen

Yeah, but that is only the "select" button, not the "play"/"run"/"execute" button

shaedrich avatar Apr 03 '24 00:04 shaedrich

The other nice thing about the idea of moving the code examples to separate Pony files, is that we easily could start including testing to prove that all the examples compile.

jemc avatar Apr 09 '24 18:04 jemc

If it helps, this can be done in a separate PR, when other questions are not yet answered

shaedrich avatar Apr 12 '24 02:04 shaedrich

@shaedrich are you interesting in tackling this? if yes, what additional information or discussion do you need from us?

SeanTAllen avatar Apr 16 '24 18:04 SeanTAllen

@SeanTAllen I am indeed. The questions asked above still stand:

  • tutorial code examples inline, as separate files (still everything in one place), or as gist?
  • playground preloaded code inline (URLs might get too long), via gist (already implemented), or via file (see above, not implemented yet, afaik)?
  • "Play"/"Run"/"Exec" button in tutorial separately below the code block (custom code) or in the top right corner as part of the editor widget (might be a little more complicated)?

shaedrich avatar Apr 16 '24 18:04 shaedrich

I'd prefer

  • separate files but...
  • it seems like gist is the only reasonable solution at this time
  • "all other things being more or less equal" i would go with "whatever you think is easiest to maintain"

my worry with gists is... that seems hard to maintain as the code is "somewhere else". which i think means, yes files is best. Looking at the tutorial I seriously doubt any of our code is going to hit a URL limit so...

I'd love to see the code stored in files and then the content put into the block at build time. then we can use ponyc to verify that all the code samples build. we can have the code inline in the block in the built site and do preloaded code inline. given that our code samples are in the hundreds to low thousands for the examples, i think that would work.

@jemc what are your thoughts?

SeanTAllen avatar Apr 18 '24 03:04 SeanTAllen

it seems like gist is the only reasonable solution at this time

Grabbing the code from files could easily be implemented here:

https://github.com/ponylang/pony-playground/blob/59b1ffdac935f27cd73d7fd77ff380e7a38e9b7d/static/web.js#L516-L527

I'd love to see the code stored in files and then the content put into the block at build time.

There's even an option to embed external files in the code block widget: https://squidfunk.github.io/mkdocs-material/reference/code-blocks/#embedding-external-files

shaedrich avatar Apr 18 '24 04:04 shaedrich

Yes, if it's possible I'd want the code to be in files in this repo (rather than gists) and get incorporated at tutorial build time. And get tested with ponyc at CI time.

jemc avatar Apr 18 '24 17:04 jemc

Status update

  1. ✅ Move code samples to separate files: #541
  2. ⏳ Fetch tutorial snippet from URL by file name: #542 and https://github.com/ponylang/pony-playground/pull/205
  3. ❓ Get pony snippets tested with ponyc at CI time → I still need to figure out, how that might work 🤔
  4. ⏳ Display code block result directly below code block on tutorial site: https://github.com/ponylang/pony-playground/pull/212 and https://github.com/ponylang/pony-tutorial/pull/543

shaedrich avatar Apr 22 '24 05:04 shaedrich