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

No output without print and no line breaks #25

Open
HugoMVale opened this issue Oct 4, 2023 · 4 comments
Open

No output without print and no line breaks #25

HugoMVale opened this issue Oct 4, 2023 · 4 comments

Comments

@HugoMVale
Copy link

Describe the bug
First of all, thanks for this very nice plugin. There are two issues, probably related:

  1. Output is only generated with print, not by calling the variable/object.

    This will produce nothing:
    '''python exec="on" source="material-block"
    x=1
    x
    '''

    This will work:
    '''python exec="on" source="material-block"
    x=1
    print(x)
    '''
    1

  2. Line breaks are not respected.

    This will print the two numbers next to each other, rather than below each other.
    '''python exec="on" source="material-block"
    x=1
    y=1
    print(x)
    print(y)
    '''
    1 2

Expected behavior
I would expect the output to be as in the console, so that the code examples are representative of real use.

System (please complete the following information):

  • Markdown Exec version: 1.6.0
  • Python version: e.g. 3.10
  • OS: Windows
@pawamoy
Copy link
Owner

pawamoy commented Oct 4, 2023

Hello, thanks for the report.

Remember that the output is treated as Markdown by default, and rendered to HTML, so line breaks will lose their effect once converted to HTML.
If you're trying to achieve similar output to the Python REPL, I suggest using pycon code blocks and the console source output:

```pycon exec="on" source="console"
>>> x=1
>>> y=2
>>> print(x)
>>> print(y)
```

That covers your second point.


I'm not sure how we could solve the first point. We'd have to somehow detect that the last line didn't use print and add it ourselves, like print(repr(x)). We'd have to make sure not to do this if the last line is an assignment, or more generally if it's output would be None. This feels super hacky 🤔

@Enter-tainer
Copy link

Enter-tainer commented May 20, 2024

Can we borrow some logic from python repl? Ideally i would expect pycon works like running python repl in console.

@pawamoy
Copy link
Owner

pawamoy commented May 20, 2024

I don't know, I never looked at its code 🙂 Would you like to investigate and send a PR?

@Enter-tainer
Copy link

Enter-tainer commented May 21, 2024

I don't know, I never looked at its code 🙂 Would you like to investigate and send a PR?

I will try. I find pycon based exec is very useful because it enables document to show (real) interactive repl session. This is especially useful when you want to write a tutorial of some library. And it can keep the output up to date -- they are generated during build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants