r/Frontend • • 3d ago

Importing Python into React components - wtf?

I didn’t realise this was possible. Christ webassembly allows for some useful things.

10 Upvotes

14 comments sorted by

11

u/Maj0rTom 3d ago

5

u/chroma_shift 2d ago

Horror? Dude this shit is straight up nightmares

1

u/sonthonaxrk 2d ago

The dataframe is actually virtualised when you scroll.

And each function is wrappped in react query so it’s properly reactive. It works unreasonably well.

And I can drop Jupyter widgets and plots into react.

30

u/LowB0b 3d ago

Christ webassembly allows for some deranged things.

ftfy

1

u/Squigglificated 2d ago

Speaking of deranged... Once many decades ago I coded ASP with PerlScript for a client. It looks something like this (fictional example):

<%@ Language=PerlScript %>
<%
my $tree = { Root => { Src => [qw(app.pl util.pm)], Docs => { Read => ['guide.md'] } } };
my $walk; $walk = sub {
  my ($node, $depth) = @_;
  return join '', map {
    my $val = $node->{$_};
    ref($val) eq 'HASH'  ? "<li class='d$depth'><b>$_</b><ul>" . $walk->($val, $depth + 1) . "</ul></li>" :
    ref($val) eq 'ARRAY' ? "<li><i>$_</i>: " . join(', ', map { s/\.(\w+)$/<sup>.$1<\/sup>/r } @$val) . "</li>" : ""
  } sort keys %$node;
};
%>
<ul data-env="<%= ($Request->ServerVariables('HTTP_USER_AGENT')->{Item} =~ /(Mobile|Bot)/i) ? lc($1) : 'web' %>">
  <%= $walk->($tree, 0) %>
  <% for my $k (grep { $_ !~ /^HTTP_/ } keys %{$Request->ServerVariables}) { %>
    <li data-key="<%= $k %>"><%= $Server->HTMLEncode($Request->ServerVariables($k)->{Item} // 'N/A') %></li>
  <% } %>
</ul>

1

u/mgr86 1d ago

Ah yes, PERL. My first programming language.

That ASP Perl concoction doesn’t look that different than some jsp I wrote where instead of the template tags i just used straight Java. 1.5 I think it was

1

u/TheRNGuy 2d ago

Is it using AST? 

It also needs code coloring.

1

u/sonthonaxrk 2d ago

It’s pure JavaScript importing Python in the browser. No AST manipulation just some decorators.

1

u/TheRNGuy 2d ago

Any ideas for practical uses? 

1

u/sonthonaxrk 2d ago

Writing a python notebook and turning it into a webpage that isn’t jank.

1

u/HKayn 2d ago

webassembly allows for some useful things

Useful for what, exactly?

1

u/sonthonaxrk 2d ago

Running Python in the browser.