TheAlgorithms / website

The Algorithms website providing GitHub's largest open-source algorithm library.
https://the-algorithms.com
MIT License
819 stars 173 forks source link

add LiveCodes as playground and test runner #269

Closed hatemhosny closed 10 months ago

hatemhosny commented 10 months ago

Closes #264

What change does this pull request introduce?

This PR adds LiveCodes as a code playground and test runner for some of the supported languages (currently JavaScript, TypeScript, Python and R). (please see #264)

Summary of approach Depending on the selected language, either LiveCodes playground or the original playground editor is loaded.
When loading the algorithm code, test code is also loaded in relevant repos (currently JavaScript and TypeScript).
In Python, doctests are run using Pyodide and the results are shown in the integrated console.

Still work-in-progress If you approve this approach, may be you want to remove the original Pyodide code runner.
Still switching themes (dark/light themes) is not implemented. However, this should be available soon.

Please let me know what you think.

By the way, I noticed that no R code is currently fetched, in addition to some other issues I noticed in fetch-algorithms script. May be I can add a separate PR for that.

Screenshots

JS thealgorithms-livecodes

Python thealgorithms-livecodes-py

Checklist

vercel[bot] commented 10 months ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
website ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 1, 2023 10:12am
maltejur commented 10 months ago

Thanks! Is there any way to make user input work? If I comment out the input code on the binary search, it throws the following error:

Screenshot_20231017_000147

With the existing playground powered by pyodide, this works as expected:

image

This is the code I used to make that work, I remember it was a bit hacky. Maybe that helps.

hatemhosny commented 10 months ago

Is there any way to make user input work?

@maltejur It works now :)

hatemhosny commented 10 months ago

Updates:

Python input Screenshot (73)

Python Result Screenshot (74)

R Screenshot (72)

Ruby input Screenshot (70)

Ruby result Screenshot (71)

hatemhosny commented 10 months ago

added Lua:

Screenshot (76)

hatemhosny commented 10 months ago

Thanks, looks great! Once you are ready I am happy to merge this.

Thanks @maltejur , I have a few things to tidy up. I will let you know when I'm ready.

One small nitpick: the autorunning of the code after ~2s of inactivity kind of clashes with the user input prompts. I am not sure though what the best solution for that would be.

The default behavior is that the code autoruns after 1.5 sec. This can be configured for longer or shorter delays. Currently, I have disabled autorunning. So, now users will have to click the run button or use the keyboard shortcut (Shift+Enter) to run the code. Please check if you think this is better or shall I re-enable the autorun?

I want your opinion about a few things:

hatemhosny commented 10 months ago

added Jupyter

Instead of displaying the notebook JSON, Python code is extracted from JSON and run using Pyodide

Pandas Screenshot (78)

Matplotlib Screenshot (77)

hatemhosny commented 10 months ago

changing themes now works

light theme Screenshot (81)

dark theme Screenshot (82)

hatemhosny commented 10 months ago

hi @maltejur

I believe the PR is ready now for review.

I would also value your opinion about these inquiries.

Thank you

hatemhosny commented 10 months ago

use LiveCodes for PHP, Go and C

these may need to manually add some code to run the algorithm code

Examples:

PHP Screenshot (83)

Go Screenshot (85)

C Screenshot (84)

hatemhosny commented 10 months ago

@maltejur I think I'm ready now if you can have a look. Thank you.

hatemhosny commented 10 months ago

@maltejur I noticed the production deployment for this failed. I cannot see the logs. Is there anything I need to fix on my end?

maltejur commented 10 months ago

That has nothing to do with your changes, it seems like there are ratelimits by GitHub which makes the fetching of some stuff fail.

RequestError [HttpError]: You have exceeded a secondary rate limit. Please wait a few minutes before you try again. If you reach out to GitHub Support for help, please include the request ID CB38:0F1F:5C9B91:BFB3B1:6546C520.

I'll try to rebuilt again tomorrow.