Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base.show(model) calling print on Jupyter #957

Closed
bbrunaud opened this issue Feb 2, 2017 · 3 comments · Fixed by #2449
Closed

Base.show(model) calling print on Jupyter #957

bbrunaud opened this issue Feb 2, 2017 · 3 comments · Fixed by #2449
Labels
Category: Printing Related to printing
Milestone

Comments

@bbrunaud
Copy link

bbrunaud commented Feb 2, 2017

Hello

Whenever a model is defined in Jupyter, the output is the full printout of the model, which can be annoying (many many lines of output). Is this the intended behavior?.

regards

Braulio

@mlubin
Copy link
Member

mlubin commented Feb 2, 2017

If something is annoying, we're likely to accept a PR fixing it.

@odow
Copy link
Member

odow commented Nov 10, 2018

Ref #1180

@odow
Copy link
Member

odow commented Feb 4, 2021

This is going to take some refactoring, because the issue is that we have a mix of print and show, a summary, a plain text model, and a LaTeX model.

In the REPL, Julia> model calls

JuMP.jl/src/print.jl

Lines 152 to 161 in a171d75

# An `AbstractModel` subtype should implement `show_objective_function_summary`,
# `show_constraints_summary` and `show_backend_summary` for this method to work.
function Base.show(io::IO, model::AbstractModel)
println(io, "A JuMP Model")
sense = objective_sense(model)
if sense == MOI.MAX_SENSE
print(io, "Maximization")
elseif sense == MOI.MIN_SENSE
print(io, "Minimization")
else

but in IJulia/Documenter, ending a cell with model calls

JuMP.jl/src/print.jl

Lines 195 to 197 in a171d75

function Base.show(io::IO, ::MIME"text/latex", model::AbstractModel)
print(io, _wrap_in_math_mode(model_string(IJuliaMode, model)))
end

but calling print(model) calls:

JuMP.jl/src/print.jl

Lines 192 to 194 in a171d75

function Base.print(io::IO, model::AbstractModel)
print(io, model_string(REPLMode, model))
end

To bring some sanity, I'd propose

  • Julia> model or ending a cell with model prints the summary
  • print(model) prints the model with all constraints in plain-text
  • print(model; latex = true) prints the latex version of the model
    • In IJulia and documenter, this pretty prints in MathJax.
    • In the REPL, this prints a string that can be copy-pasted into a tex file.

The only breaking change for this from a user-perspective is that models in IJulia would show a summary by default, and you'd have to use print(model; latex = true) to get the latex version.

Proof-of-concept

struct Model end

function _print_summary(io::IO, model::Model)
    print(io, "model summary")
end

function _print_model(io::IO, model::Model)
    print(io, "full model")
end

function _print_latex(io::IO, model::Model)
    print(io, "\$\$\\min Model + latex\$\$")
end


Base.show(io::IO, model::Model) = _print_summary(io, model)

struct LaTeXModel{T}
    model::T
end

function Base.show(io::IO, model::LaTeXModel)
    return print(io, sprint(_print_latex, model.model))
end

function Base.show(io::IO, ::MIME"text/latex", model::LaTeXModel)
    _print_latex(io, model.model)
end

function Base.print(io::IO, model::Model; latex::Bool = false)
    if latex
        return LaTeXModel(model)
    else
        _print_model(io, model)
    end
end

Base.print(model::Model; latex::Bool = false) = print(stdout, model; latex=latex)

Output from IJulia

image

Output from the REPL

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Printing Related to printing
Development

Successfully merging a pull request may close this issue.

4 participants