I've gotten a task at University to create a website for teaching other students how to use python libraries (Scrapy, BS) for web scraping. I've already set up the frontend part with React and React Ace editor and now I'm trying to figure out how pass code that user entered to the script. Do I need to make my own server (api) or I can call the python script directly and pass all the code using ajax request?
What is the best approach?
What I want is to pass all the code that user entered in the online editor to the python script and then return the output of that script as a response to the initial request, so that I could check whether the result is correct.
my suggestion is to create a server with API (it's actually AJAX)
and then to run the Python code,
one good option us to create express app (Node.js HTTP server) and use python-shell see the first example with runString option.
you will need a server with node.js and python installed.
of course this only for a university task, in this option you run the code on your server, and it's a pretty big security issue.
Related
I have this node script where it does an API call and receive the response. So what I want to do is to make a UI that controls when the script works. For example when I click a button (HTML) the script starts and then prints the response into a textbox. Is there any way to do this?
You need something to render the HTML and send a message to your Node.js program and your Node.js program needs some way to understand that message.
The most common approach would be to write a web service (usually using Express.js unless you are using React/Vue in which case Next.js/Nuxt become more interesting) and then communicate with it using Ajax (typically the fetch API). Other options would be form submissions or web sockets.
Less common would be to use a framework such as Electron.js or OpenFin to run a desktop application with an embedded HTML renderer. They then have their own APIs to communicate with the Node.js portion of the application.
for example:
If i am using an npm package, i can only use it on server side js. Does that mean one will always have to send data through ajax post/get request for example to server side do the calculation then send data back? Is there a better way to accomplish this?
I am aware of how react works etc and i am aware of workarounds to get require to work browser side, but for sake of understanding workflow and how it should all be set up I ask this question.
I am currently using express with nodejs and using ajax calls to talk to server side js and send info back. So want to know if there is a better way of doing this?
No, there's no "better" way to send data from the client browser to the server than an ajax call. Alternativelly you can use a normal form, or url parameters to pass the server some variable with the page load request.
Depending on the npm package you are using to do your calculations, you can just serve that .js library up along with your .html page by moving that script into a static assets folder and referencing it with a <script> tag. Then, you can do the calculation entirely in the browser (client-side) assuming you don't need any other I/O (db, etc.).
I have made a simple password protected webpage that provides the link to another webpage when we enter the correct password but we can easily have the webadrress of other page by browsers "View Page Source" feature.So how we can overcome this?
The problem is that you are keeping your secret data on the client and client can easily reach it. So, the only posible way for you is to keep them on the server. But, as far as I understand you need help with server part. So here is small instruction with links to the documentation.
You will need:
Some backend that will take data from your users and returns some responce. I would suggest you to have a look into Node.Js as a platform with Express as a server. Benefit from this setup is that Node.Js uses JavaScript so you have not to learn additional language, and Express is a very simple server to use. Of course, if you want to learn some other languages - you can take C# with ASP.NET MVC framework, PHP or any other nice language.
Some page with form for user's credentials that will post data to your backend. Basic form behavior can be found here
And some code on server that will validate credentials from form and return new page with or without your secret.
That's it. May be it sound a bit scary but there are lots of guides and information your can google.
Hope this helps. Happy coding!
Btw, if you are lloking for the some ready code, I have Node.JS client/server example with TypeScript (JavaScript with type validation) here. All you need is git, hope you already have it, and Node.Js
Then just execute this commands on your console (bash, cmd, etc)
git clone https://github.com/Drag13/typescript-browserify-template
This will download code from remote server to your local machine
npm install
Installs project dependencies - like express server
npm run server
Starts the server
cd..
npm run client
Starts the client
Maybe you will find this helpfull.
sorry to bother you, but I've been looking for answers and I couldn't find them anywhere... Well i'm a fresh dude in the field of javascript and node.js and here's my problem:
I've created an application based on the tutorial of socket.io - Here's the link to the completed project of their chat example. everything is working as it should, but I would really need to trigger somekind of a command while node server is running... command should be triggered via php script.
The command should trigger an emit event - so every client in our case would see a new message sent via php.
I saw couple of suggestions to do it from another server with php/using cURL. The problem is that I don't know how to fetch POST data sent from php to node.js server.
Any solution to command node with php is more than welcome and again i'm sorry to bother you :)
You can use a bridge to exchange data between NodeJS and PHP. You will need to implement a PHP server that exposes remote procedures and a NodeJS client that calls those remote procedures. You can send any data to the NodeJS application from PHP using these remote procedures.
Here are a few links to get you started:
http://bergie.iki.fi/blog/dnode-make_php_and_node-js_talk_to_each_other/
https://github.com/bergie/dnode-php
So I was learning JavaScript/jQuery and building a website as I go along. Now I need to have a database, and my friend recomended learning Ruby and using it to handle data. From this question: JavaScript Execute Ruby Script It shows how I could execute the Ruby script, but I was wondering if it is possible to send data to that script so it can push it to a MySQL database?
Basically the user would submit a string, and all the stuff is dynamically generated by JS, so I want to send that string to the SQL db as soon as the user submits it. If anyone can point me in the right direction in terms of readings. I don't have much knowledge/experience in Ruby so anything that redirects me to something useful for this particular task would be great. Thanks!
The question you referred to is probably not what you want. That will only work if the server is on the same machine as your browser and if that machine runs Windows.
For a website, that everybody can visit you need to have a server that runs some software - in your case written in ruby - and that is sent requests by the browser - in your case through your JavaScript program.
To do that you need to send an XMLHttpRequest. For jQuery you can read about this in the docs or in a tutorial. This way you get your browser to talk to the server.
For the server to listen and respond you should use some framework like Ruby on Rails or (not Ruby but Python) Django.