fonsp / Pluto.jl

🎈 Simple reactive notebooks for Julia
https://plutojl.org/
MIT License
5.01k stars 293 forks source link

Suggest importing package when using an undefined function #2833

Open aplavin opened 9 months ago

aplavin commented 9 months ago

Pluto pioneered automatic package installation in Julia: just type using PackageName, and it's already there!

Over the recent years, Julia REPL got features that make even more seamless exploratory experience possible. Julia itself provides auto-installation now (with a confirmation prompt), and autoloading can be added with a short startup.jl. Here's mine startup.jl, and below is a realtime recording of how it works. All installations are done into a temp env.

Would be nice if an undefined function/macro name is detected, Pluto looked up a list of common packages and symbols, and suggest to add using ThisPackage to the notebook!

asciicast

fonsp commented 8 months ago

Nice! I think it's good to have a curated list of packages that can be auto-installed (instead of an auto search of all registered packages), just like in your case:

        :StaticArrays => [:SVector, Symbol("@SVector")],
        :Statistics => [:mean, :median, :std, :cor, :cov, :quantile],

How should we go about writing this list? How do we decide what goes in and what doesn't?

In your case the feature gives a faster dev experience in the REPL "for the pros", but what I find most exciting is that it can help Julia beginners. I think it's common to copy-paste a code snippet (eg from ChatGPT) that contains a call to a popular function like plot or mean and it would be cool if Pluto could help with that, instead of just "plot not defined, did you mean splat?"

aplavin commented 8 months ago

plot is one of the best/worst examples here, depending on how you look at it :) There are so many options: Plots, Makie (Cairo or WGL), PyPlot, ....

I think this autoinstall feature has to involve a prompt anyway, it shouldn't be fully automatic. So, if I type plot() or weights(), I don't really want to wait for some package installation – maybe I just want to define a function with this name myself in the notebook. Then the question is, what should be in that prompt.

aplavin commented 8 months ago

Maybe, "prompt" could be as simple as Pluto adding a cell above the executed one, with the content like

using StatsBase
using # Plots, CairoMakie, WGLMakie, PyPlot, PythonPlot – uncomment one name here

The cell should just be added automatically, but not executed. Then the user either just checks it and runs, or decides which of the "ambiguous" packages they want and then runs the cell.