Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

initial work#1

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
rmorshea wants to merge5 commits intoreactive-python:main
base:main
Choose a base branch
Loading
fromrmorshea:initial-work

Conversation

rmorshea
Copy link
Contributor

@rmorshearmorshea commentedMay 21, 2023
edited
Loading

Summary

A plugin for MkDocs that displays live ReactPy views.

Basic usage:

#Hello World<reactpy-framefile="path/to/script.py" />

Then intest.py

fromreactpyimportcomponent,html,run@componentdefexample():returnhtml.p("hello from reactpy")run(example)

Then, when runningmkdocs server, this would render:

<h1>Hello World</h1><p>hello from reactpy</p>

All this is accomplished by injecting a simple JS bundle into the static site that sets up areactpy-frameweb component.

Todo

Stuff we still have to figure out. Maybe we create issues for these?

Running in Production

Figure out a reasonable way to set this up in production. Right now, whenmkdocs serve is run, we spin up a sidecar ReactPy server next to the MkDocs dev server. In production though we may want areactpy-mkdocs serve command that runs a production ASGI server that (optionally) will serve the static site generated bymkdocs build.

Displaying Code Samples

The current ReactPy docs have a nice mechanism for displaying code from various files along with the rendered result:

Peek 2023-05-21 01-45

We should figure out a way to reproduce this. Perhaps the API would look like:

<reactpy-block><reactpy-filename="main.py"/><reactpy-filename="data.json"/></reactpy-block>

Maybe there's some way to do this withMkDocs tabs andcode blocks.

@rmorshearmorshea requested a review fromArchmongerMay 21, 2023 07:46
@Archmonger
Copy link

Archmonger commentedMay 21, 2023
edited
Loading

The concept of this is clever.

I'll get some time to do a review tomorrow night. But based off the PR description, one comment I do want to make is that mkdocs users are more used to Jinja tags rather than HTML tags.

{%reactpyfile='path/to/file'%}# tag name could just be reactpy

By the way, the tabbed sections are a thing in mkdocs-material.

@rmorshea
Copy link
ContributorAuthor

rmorshea commentedMay 21, 2023
edited
Loading

We could add a Jinja tag, but it may be an unnecessary layer of indirection since the tag would probably just render as a<reactpy-frame> anyway. And yeah, I saw the tabbed sections in mkdocs-material. I just wasn't sure how to use them from within this plugin.

@Archmonger
Copy link

Here's asimilar plugin, except it's designed to inject arbitrary HTML/Markdown.

@rmorshea
Copy link
ContributorAuthor

I wonder if we can just depend on that plugin and just leverage some of its utilities inside ours.

@rmorshea
Copy link
ContributorAuthor

If we end up needing a jinja tag to get the tabbed code examples working then we may as well just use a jinja tag for the simple embedded view as well, even if it's just an alias for the web-component, in order to keep things consistent.

@Archmonger
Copy link

Archmonger commentedMay 21, 2023
edited
Loading

Everything in mkdocs compiles to HTML so we won't need jinja tags to get tabs working.

Jinja tags are more of a creature comfort thing. But I've never seen a mkdocs plugin that makes the user write raw HTML.


log = getLogger(__name__)

REACTPY_RUN = reactpy.run

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Needs a comment to describe why this is being declared as a global, rather thanfrom reactpy import run as REACTPY_RUN

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think this can be renamed toORIGINAL_REACTPY_RUN too. We patch this on the fly below so wecan capture components from example code.

log.error("Multiple files specified in query string")
return None

return load_file_view(query["file"][0])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Security flaw: this can result on directory traversal attacks. We need to make sure we're not leaving the safe directory.

rmorshea reacted with thumbs up emojirmorshea reacted with eyes emoji
@rmorshea
Copy link
ContributorAuthor

rmorshea commentedMay 23, 2023
edited
Loading

Ok, so I don't think this syntax will work:

<reactpy-block><reactpy-filename="main.py"/><reactpy-filename="data.json"/></reactpy-block>

Ultimately this would require me to manually construct the tabs in Javascript. While reverse engineering the DOM structure/classes created by the mkdocs-material tabs is possible, that seems like it would be brittle and a lot of work.

I'm thinking that we could piggyback onsuper-fences. This would allow us to just write:

```reactpyfrom reactpy import component, html, run@componentdef example():    ...run(example)```

Perhaps then, for tabs, one could write:

```reactpy { tab="main.py" }# python code``````reactpy { tab="data.json" }{"some": "data"}```

Where consecutive tabbed reactpy code-fences would be grouped together.

The implementation details are a little unclear to me, butcustom fence formatters do seem to receive the main markdown parser as one of their parameters so I'm hoping that I can just convert the reactpy code-fences into standard markdown and ask it to parse that for me. For example, the tabbed code fences might get transformed into:

=== "main.py"    ```python    # some code    ```=== "data.json"    ```json    {"some": "data"}=== "Preview"    <reactpy-frame file="path/to/main.py" />

@Archmonger
Copy link

Archmonger commentedJun 14, 2023
edited
Loading

Based on ouroriginal plan from half a year ago, we were considering doing the embeds viamkdocs-macros-plugin.

I thinkmkdocs-macros would be the cleanest option overall.

@rmorshea
Copy link
ContributorAuthor

The advent ofthis pyscript demo means we can take an entirely different approach to running live examples in our docs.

@Archmonger
Copy link

A macro that poops out a pre-generated pyscript template would be pretty simple to make.

rmorshea reacted with thumbs up emoji

@Archmonger
Copy link

Archmonger commentedJul 27, 2023
edited
Loading

The template tag usingmkdocs-macros would look like this:

{% reactpy "../../examples/thinking_in_react/build_a_static_version_in_react" %}

Ifbuild_a_static_version_in_react isn't a folder, we should use pre-defined tab names. For example:

=== "app.py"```pythonPYTHON CODE GOES HERE FOR "../../examples/thinking_in_react/build_a_static_version_in_react.py" IF IT EXISTS```=== "styles.css"```cssCSS CODE GOES HERE FOR "../../examples/thinking_in_react/build_a_static_version_in_react.css" IF IT EXISTS```=== "data.json"```cssJSON CODE GOES HERE FOR "../../examples/thinking_in_react/build_a_static_version_in_react.json" IF IT EXISTS```=== ":material-play: Run"PYSCRIPT CODE GOES HERE. FOR PERFORMANCE, PYSCRIPT CODE SHOULD NOT RUN UNLESS THIS TAB IS CLICKED.

If"../../examples/thinking_in_react/build_a_static_version_in_react" is a folder, we should create tab names based on the files within that folder, rather than generics names.

Inside of python files it should automatically look for any# start and# end. If they exist, truncate the code block.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@ArchmongerArchmongerArchmonger requested changes

Requested changes must be addressed to merge this pull request.

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@rmorshea@Archmonger

[8]ページ先頭

©2009-2025 Movatter.jp