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

how to make sample code runnable ? #333

Open
lucemia opened this issue Feb 26, 2024 · 2 comments
Open

how to make sample code runnable ? #333

lucemia opened this issue Feb 26, 2024 · 2 comments
Labels
Type: Feature New Feature

Comments

@lucemia
Copy link
Contributor

lucemia commented Feb 26, 2024

Screenshot 2024-02-26 at 12 58 40 PM (2)

@lucemia lucemia added the Type: Feature New Feature label Feb 29, 2024
@lucemia
Copy link
Contributor Author

lucemia commented Mar 18, 2024

https://github.com/samuelcolvin/mkdocs-run-

The configuration that makes the code examples runnable in your MkDocs setup is the inclusion of an external JavaScript file specifically designed for making code blocks interactive:

extra_javascript:
  - 'extra/fluff.js'
  - 'https://samuelcolvin.github.io/mkdocs-run-code/run_code_main.js'

In this part of your MkDocs configuration, the run_code_main.js script from the URL https://samuelcolvin.github.io/mkdocs-run-code/run_code_main.js is particularly important. This script is part of the mkdocs-run-code plugin or a similar setup, which enables the functionality to run Python code directly from the MkDocs pages in the browser. This JavaScript file, when included in your MkDocs site, interacts with code blocks (typically within specific HTML elements) to make them interactive, allowing users to execute the code examples shown in your documentation.

Ensure that you have set up everything necessary for the mkdocs-run-code plugin or the respective tool you're using, as this involves more than just including the JavaScript file. This might include additional setup on the server hosting the documentation or configuring specific options for how the code should be run and displayed. If mkdocs-run-code is what you're using, you should also ensure that it's properly installed and configured as part of your MkDocs plugins.

@lucemia
Copy link
Contributor Author

lucemia commented Mar 18, 2024

from datetime import datetime

from pydantic import BaseModel, PositiveInt


class User(BaseModel):
    id: int  # (1)!
    name: str = 'John Doe'  # (2)!
    signup_ts: datetime | None  # (3)!
    tastes: dict[str, PositiveInt]  # (4)!


external_data = {
    'id': 123,
    'signup_ts': '2019-06-01 12:22',  # (5)!
    'tastes': {
        'wine': 9,
        b'cheese': 7,  # (6)!
        'cabbage': '1',  # (7)!
    },
}

user = User(**external_data)  # (8)!

print(user.id)  # (9)!
#> 123
print(user.model_dump())  # (10)!
"""
{
    'id': 123,
    'name': 'John Doe',
    'signup_ts': datetime.datetime(2019, 6, 1, 12, 22),
    'tastes': {'wine': 9, 'cheese': 7, 'cabbage': 1},
}
"""

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

No branches or pull requests

1 participant